I'm using a Mac and I have this alias defined in .bashrc
:
$cat .bashrc | grep la
alias la='ls -la'
then I try to use it in a script:
$cat ./mytest.sh
#!/bin/bash
la
It runs and says it cannot find la
:
./mytest.sh: line 2: la: command not found
Why is this? I tried on both Mac and Linux, same error!
The simplest answer is to fix this issue is to do the 2 important things in your script -or it wont' work, if you just do one thing.
After this, your aliases that you have defined in ~/.bashrc they will be available in your shell script (giga.sh or any.sh) and to any function or child shell within such script.
If you don't do that, you'll get an error:
Your
.bashrc
is only used by interactive shells. https://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files says:As you can see, there's nothing about
.bashrc
there. Your alias simply doesn't exist in the script.But even if
.bashrc
were read, there's another problem:So if you wanted aliases to work in a script, you'd have to do
shopt -s expand_aliases
first. Or just use a shell function instead of an alias:At the beginning of the ~/.bashrc file usually can be found two lines as:
This line aborts the inclusion for scripts which is not recommended anyway. For portability issues, you usually write the full command or define the alias in the script itself.