My .bashrc
sets up a bunch of aliases for me to use as needed, and then runs one of them automatically.
Turns out this caused some problems with automated scripts ssh-ing into my machine, as opposed to using an interactive shell. So, to fix this, I put them inside an if block so they wouldn't be defined or run for those automated scripts...
if [ -n "$TERM" ] && [ "$TERM" != "dumb" ] ; then
alias short='long command here'
alias another='very long command here'
# ...
short
fi
Only to see short: command not found
!
Let's reduce this to the minimum...
$ cat alias.sh
alias foo='echo hi'
foo
$ sh alias.sh
hi
cat alias-in-if.sh
if true ; then
alias foo='echo hi'
foo
fi
sh alias-in-if.sh
alias-in-if.sh: line 3: foo: command not found
Why does the first script work, and not the second?
(I've answered my own question.)
bash
won't work even with the non-ifalias.sh
. I guess you tried it on an ubuntu or debian system wheresh
isdash
, notbash
(Inbash
, you'll have to useshopt -s expand_aliases
in scripts).sh
or with the--posix
switch turns that on (since the standard makes no allowances for not expanding aliases in scripts). You'll have to turn it on explicitly if your script is using a#! /bin/bash
shebang, though.bash(1)
manpage, but in the texinfo manual, which is not installed by default on many distros.