I have this simple bash function:
function sb() {
# Starts a process in the background, suppressing any stdout / stderr output.
$* >/dev/null 2>&1 &
}
I can use it to start some process in the background, for example: sb ping google.com
, and when I type jobs
, I can see the job and its id, and I can use fg <id>
to bring it to the foreground. But if I pass an alias to this function, for example the alias alias ping-google="ping google.com"
I can't see it in the jobs list. Why? Are there any other way to find its id and bring it to the foreground?
CodePudding user response:
From https://www.gnu.org/software/bash/manual/html_node/Aliases.html:
Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. [...] This behavior is also an issue when functions are executed. Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a command. As a consequence, aliases defined in a function are not available until after that function is executed. To be safe, always put alias definitions on a separate line, and do not use alias in compound commands. For almost every purpose, shell functions are preferred over aliases.