3

Have a situation where docker run .... is "wrapping" an unknown number of potential arguments so we can't use a function. If you're curious: https://github.com/dbt-labs/dbt-core/tree/main/docker#running-an-image-in-a-container

So I want to alias the docker run .... part of the docker run..... {real command} command.

alias my_command="docker run -e PASS='mypass' --some-setting" 

'mypass' needs to be in single quotes.

With the above alias, alias my_command yields

alias mycommand='docker run -e PASS='\''mypass'\'' --some-setting' 

And this doesn't execute the right command. Specifically we get a permission denied. Running the exact same command straight, without the alias, works:

docker run -e PASS='mypass' --some-setting 
7
  • 5
    Use a function instead , my_command() { docker run -e PASS='mypass' --some-setting "$@"; } Enjoy... Commented Jan 10, 2023 at 0:52
  • Ahhh, sweet. Didn't know about "$@". Thought we had to do $1, $2, .... Thanks! Commented Jan 10, 2023 at 1:05
  • 1
    It will expand to all the positional parameter, at least whats left of it to expand. Commented Jan 10, 2023 at 1:09
  • See PAGER='less +/^\ *@' man bash Commented Jan 10, 2023 at 1:21
  • 2
    The output from alias my_command looks weird, but it's actually correct (at least, the part you gave looks correct). That weird '\'' thing is one way to put a single-quote in a mostly-single-quoted string (which is how it's displaying the alias). See Nick Jensen's answer here. Is there something in the password itself that's wrong in the alias my_command output, or is it just the weird-looking quoting? Commented Jan 10, 2023 at 2:50

1 Answer 1

2

Quotes (' or ") and escape characters (\) are not part of a value; they are syntax and
solely instruct your shell to not perform any word splitting or parameter expansion.

Thus, if you have a command containing -e PASS='my pass', you could (re)write the command with the following equivalent forms – your shell won't care and your invoked binary cannot even find out which form was used:

  • -e 'PASS=my pass'
  • -e PASS=my' 'pass
  • -e PASS=my\ pass
  • -e 'PASS'="my pass"
  • '-e' "PASS"='my'\ "pass"

(yes, all of those are equivalent; identical even, after the shell has parsed them).

Consequently, you cannot use alias if your command is more complicated than the simplest and most trivial commands. Anything more complex (multi-word tokens, quotes, etc.) and you are better off defining a shell function to invoke the command for you. Shell functions have precedence over binaries so you can give them the name of an existing binary and your function would be invoked.

mycommand() { docker run -e 'PASS=my pass' --some-setting "$@"; } 

"$@" expands to all positional parameters, but preserves the word-splitting of the original parameters. Invoke this shell function as follows:

mycommand --flag --option-with-value='value with spaces' 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.