3

I am running ubuntu using WSL (Windows Subsystem for Linux). I have written a simple shell script below. When I run it gives the below error.

#! /bin/sh
# "test script"
echo "hello world"

test.sh: command not found

But it runs successfully If I run the using command sh test.sh or bash test.sh.

So is there any way I run without sh like the native Linux?

2

2 Answers 2

1

You can always run it as ./test.sh.

./ resolves to current directory ending with /, so you're effectively providing the full path to your script.

Just test.sh will work only if your current directory is in $PATH. This isn't exclusive to WSL.

2
3

Basics:

First:
You create a script with a "shebang" on the first line, this defines if it is e.g. bash, python or perl which understands the remainder of the text file.

This usually looks like: #!/bin/bash, #!/bin/env python or some such

Second:
Make sure the file has the x -flag for a selection of user, group and/or others (man chmod for more detail) by use of e.g.
chmod 755 script_filename

Third:
Place it in a folder (directory) on your harddisk, on any permanently mounted storage medium.
Here mkdir -p $HOME/bin/ might be a good idea, for personal stuff.

Fourth:
make sure the folder is stated in echo $PATH - by e.g. adding a line containing
export PATH=$PATH:/absolute/path/to/folder/
in $HOME/.bash_aliases

With all these in place your script will be accessible (executable) - in a newly launched shell (for #4 to take effect) - by only typing the name at the bash prompt.
(Note: WSL at least in first incarnation, is/was an Ubuntu by Canonical)


More detail: https://tldp.org/guides.html - in the Bash guides

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .