0

This question already has an answer here:

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.

marked as duplicate by muru bash Jan 22 '18 at 7:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Aliases are disabled by default in scripts. You need to enable them (the shopt command in Panther's answer). – muru Jan 22 '18 at 7:48
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".

  • Hmm, is there anything to handle/bypass this behavior and get the desired behavior?... – pntshere Jan 22 '18 at 6:03
  • 1
    You can write the aliases/variables to a temporary file(s) then read that in. I don't know of any other way. – stumblebee Jan 22 '18 at 6:41
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.