Never use ~ in your $PATH (/bin/sh might not expand it)
dash
doesn’t exand ~
when it occurs in $PATH
, so if you want your scripts or environment to be portable across shells, you should never use ~
in your $PATH
.
/bin/sh
is dash
in Debian and in Ubuntu, so you’re most likely to encounter this problem when using the #!/bin/sh
shebang on those operating systems.
To demonstrate this behavior on by Debian Stretch machine, let’s create a helloworld
script in ~/
and take a look at our $PATH
:
$ cd ~
$ echo 'echo "hello world!"' > ~/helloworld
$ chmod +x ~/helloworld
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
bash
expands ~
when it occurs in $PATH
:
$ PATH="$PATH:~" bash # run bash with ~ on path
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:~
$ echo ~
/home/ggustafson
$ helloworld
hello world!
But dash
doesn’t:
$ exit # leave bash with ~ on path
exit
$ PATH="$PATH:~" dash # run dash with ~ on path
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:~
$ echo ~
/home/ggustafson
$ helloworld
dash: 3: helloworld: not found