0

I have executed a remote Bash script bash <(curl -s URL/script.sh | tr -d '\r') that does the following 3 actions:

  1. Downloads another script with a list of aliases (aliases.sh) to current dir.
  2. Executes a source ./aliases.sh command.
  3. Executes one of the aliases in the list after their script was sourced. This alias is rss, aimed to restart the webserver service.

For some reason, alias execution by the script always fails, and yet, manual execution always succeeds.

The current state is that the rss alias (and all other aliases in he list actually) would be ignored and considered "nonexistent" when being executed by the script.sh even though their file aliases.sh is sourced and execution of rss and all other aliases succeeds perfectly if I execute them manually (typing rss and hit Enter).

Why is this "execution discrimination", so that script alias execution fails but manual alias execution succeeds?

I've reproduced this in a few different Ubuntu environments. I can't explain this and would appreciate your take on this.

  • Aliases are disabled by default in scripts. You need to enable them (the shopt command in Panther's answer). – muru Jan 22 '18 at 07:48

2 Answers2

1

If you are running shell script "b" from shell script "a" all aliases and variables set in script "b" is only good for that shell environment, they do not get passed back to shell script "a".

stumblebee
  • 3,547
1

Aliases are made for interactive usage. In a script you can get the same result (as you get from the alias) by entering the corresponding command into the script itself.

If you want to use it several times, you can create a function, and call it in the script.

Example:

alias alidr='sudo lsblk -o model,name,size,fstype,label,mountpoint'

can be replaced in a bash script with

function flidr {
 sudo lsblk -o model,name,size,fstype,label,mountpoint $*
}

and it can be called in the script with

flidr

or to show only specified drives with parameters

flidr /dev/sda /dev/sdb
sudodus
  • 46,324
  • 5
  • 88
  • 152