I opened “Terminal” from my MacBook Pro, and try to run some bash script from book “Bash Guide for Beginners”:
first I checked the PATH environment setting and current working folder, which contains a script “script1.sh”:
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$ pwd
/Users/liuren/Documents/myscript
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$ ls
script1.sh
then I set the PATH to include current working folder, and it seems succeeded:
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$ export PATH="$PATH:/Users/liuren/Documents/myscript"
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/liuren/Documents/myscript
however I can’t get the “script1.sh” running:
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$ script1.sh
#: bad interpreter: No such file or directoryt1.sh: /bin/bash
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$ ./script1.sh
#: bad interpreter: No such file or directory
The script is actually very simple, from “Bash Guide for Beginners”:
#!/bin/bash
# This script clears the terminal, displays a greeting and gives information
# about currently connected users. The two example variables are set and displayed.
clear # clear terminal window
echo "The script starts now."
echo "Hi, $USER!" # dollar sign is used to get content of variable
echo
echo "I will now fetch you a list of connected users:"
echo
w # show who is logged on and
echo # what they are doing
echo "I'm setting two variables now."
COLOUR="black"
VALUE="9"
echo "This is a string: $COLOUR"
echo "And this is a number: $VALUE"
echo
echo "I'm giving you back your prompt now."
echo
, and there is actually “/bin/bash”:
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$ cd /bin
unknown_b8-8d-1b-3d-0b-3d:bin liuren$ ls
[ df launchctl pwd tcsh
bash domainname link rcp test
cat echo ln rm unlink
chmod ed ls rmdir wait4path
cp expr mkdir sh zsh
csh hostname mv sleep
date kill pax stty
dd ksh ps sync
unknown_b8-8d-1b-3d-0b-3d:bin liuren$
Using other ways to run it, it shows no result
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$ source script1.sh
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$ bash script1.sh
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$ bash -x script1.sh
I guess it didn’t work. How can I fix this issue?
ps. what is this “unknown_b8-8d-1b-3d-0b-3d”, is it possible to change it to a more meaningful thing?
I’ve seen this before when a shebang line has DOS-style line endings (usually when it was created on a Windows PC).
If you do
cat -vet ./script1.sh, you will probably see:Remove those carriage returns and you’ll be set.
One way to do this is:
…which will create a new file (./script2.sh) without the carriage returns.
If you make
./script2.shexecutable (chmod +x ./script2.sh), you should be able to run it (without needing to update${PATH}, it’s usually a bad plan to have the current directory in your${PATH}‘,To find out all of the elements of your prompt (
unknown_b8-8d-1b-3d-0b-3d:myscript liuren$), et al, use:This will contain characters like
\w, which are expanded to the current working directory, for example.You can see a full list of these codes in the Bash Reference Manual.
Happy scripting!