linux - Create alias using bash one liner -


i need create alias long command, use

alias mylongcommand="my long command" 

but need define in online system configuration can use bash command, tried various combinations including:

bash -c 'alias mylongcommand="my long command"' 

but unfortunately no success, alias not defined.

to test success use

bash -c mylongcommand 

short answer: use exported function instead

# define function mylongcommand() { long command; }  # export function environment export -f mylongcommand  # thereafter, function available in subprocesses bash -c 'mylongcommand' 

...or, compress actual creation one-liner:

mylongcommand() { long command; }; export -f mylongcommand 

note definition , export needs done in bash shell parent process of shell in intend function used. if use bash -c 'mylongcommand() { long command; }; export -f mylongcommand', environment variable exported function lives long bash -c instance -- hence, it's entirely unavailable in subsequent command.

if wanted available bash shells in login session, put definition , export in ~/.bash_profile (and log out , in), if system behaving per typical defaults respect dotfile initialization.

longer answer: why did original attempt fail?

multiple reasons:

  • aliases part of state of individual shell process -- there isn't shared registry. thus, bash -c 'alias foo="bar"' sets alias only single bash instance created bash -c command, , alias terminated when command exits.
  • noninteractive shells, such created bash -c, don't inherit aliases parent processes, or read dotfiles (by default), start out alias table empty, if alias defined in (separate) shell that's invoking them.
  • noninteractive shells, such created bash -c, have alias evaluation turned off default, intended interactive-use feature.

longer answer: why not alias?

aliases are intended facility interactive use. thus, they're not default available inside noninteractive shells @ all. enable alias expansion in noninteractive instance of bash, 1 needs run:

shopt -s expand_aliases 

the other issue getting alias defined @ all. noninteractive shells don't run dotfiles default (the name of dotfile run in noninteractive shell can placed in env or bash_env environment variable override this), won't have aliases defined in ~/.bashrc present.

longer answer: ...so, how could use alias?

# create .env file, taking care not fail badly if shell not bash # note making noninteractive shells emit content on stderr break software # ...so redirecting errors entirely essential here, , ||: necessary not break # ...any noninteractive shell (like dash or ash invocations) doesn't support shopt # ...when shell invoked -e argument. cat >~/.env <<'eof' shopt -s expand_aliases 2>/dev/null ||: "fail gracefully w/o shopt" alias mylongcommand="my long command" 2>/dev/null ||: "fail gracefully w/o aliases" eof  # point .bash_profile cat >>~/.bash_profile <<'eof' export env=$home/.env [[ -e $env ]] && source "$env" eof  # ...and update already-running shell: export env=$home/.env source "$env" 

...to above in 1 line:

printf '%s\n' 'shopt -s expand_aliases 2>/dev/null ||:' 'alias mylongcommand="my long command" 2>/dev/null ||:' >~/.env; printf '%s\n' 'export env=$home/.env' '[[ -e $env ]] && source "$env"' >>~/.bash_profile 

Comments