Home > OS >  ZSH: Escaped space chars in command args variable
ZSH: Escaped space chars in command args variable

Time:01-29

I transitioned from bash to zsh recently and found that some basic stuff appears to work differently. I'm on Mac OSX.

In my bash profile, I had this:

alias rununit='python -m pytest --no-cov tests/unit --disable-pytest-warnings --durations=1 --show-capture=no'
export short='--tb=short -qqq'

Then I could run rununit $short and it would run pytest with the additional args in $short. This works fine in bash, but it breaks in zsh. Apparently the space between short and -qqq is getting escaped, so pytest interprets the value of tb arg as short -qqq which obviously breaks things.

Is there any way to make this handy shortcut work in zsh?

CodePudding user response:

zsh does not apply word-splitting to a parameter expansion by default, so rununit $short behaves the same as rununit "$short".

Make short an array instead. (It does not need to be exported.) I would make rununit a function as well.

rununit () {
  python -m pytest --no-cov tests/unit --disable-pytest-warnings --durations=1 --show-capture=no
}

short=(--tb=short -qqq)

Because of how zsh handles parameter expansions, this will make rununit $short behave the same as rununit "${short[@]}" (which is what you should use in bash as well).


Or, make short an option that the function rununit processes to add additional options to your command:

rununit () {
  args=(--no-cov tests/unit --dsiable-pytest-warnings --durations=1 --show-capture=no)
  if [[ $1 = short ]]; then
      args =(--tb=short -qqq)
  fi
  
  python -m pytest $args
}

Now you can run rununit or rununit short.

  •  Tags:  
  • Related