Skip to content

Instantly share code, notes, and snippets.

@minrk
Created February 23, 2018 16:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minrk/527a11b2442d7b3188ed7880cbda6d7c to your computer and use it in GitHub Desktop.
Save minrk/527a11b2442d7b3188ed7880cbda6d7c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# show-args is a Python script that prints sys.argv[1:]
echo '$@ =' "$@"
echo 'no quotes: $@'
show-args $@
echo 'quotes: "$@"'
show-args "$@"
AT="$@"
echo 'quotes: "$AT" where AT="$@"'
show-args "$AT"
echo 'no quotes: $AT'
show-args $AT
echo "HOW DOES THAT WORK"
@minrk
Copy link
Author

minrk commented Feb 23, 2018

output:

$@ = has space arg 2
no quotes: $@
['has', 'space', 'arg', '2']
quotes: "$@"
['has space', 'arg 2']
quotes: "$AT" where AT="$@"
['has space arg 2']
no quotes: $AT
['has', 'space', 'arg', '2']
HOW DOES THAT WORK

@swfiua
Copy link

swfiua commented Feb 23, 2018

The section in the bash man page on quoting may help.

Short version:

double quotes ": shell expansion still happens
single quotes ': no shell expansion

@mpacer
Copy link

mpacer commented Feb 23, 2018

could you add show-args as a file to this gist? I wanted to reproduce this exactly and I'm finding it tricky to execute show-args as a script without creating a full package to declare it as an console_script entrypoint… unless that's actually what you did to get that script to work without explicitly in.

Is it more than:

#!/usr/bin/env python

if __name__ == __main__:
    import sys
    print(sys.argv[1:])

Also… even modifying it to :

#!/usr/bin/env bash
# show-args is a Python script that prints sys.argv[1:]
echo '$@ =' "$@"
echo 'no quotes: $@'
python show-args.py $@
echo 'quotes: "$@"'
python show-args.py "$@"
AT="$@"
echo 'quotes: "$AT" where AT="$@"'
python show-args.py "$AT"
echo 'no quotes: $AT'
python show-args.py $AT
echo "HOW DOES THAT WORK"

I'm still only getting

$@ =
no quotes: $@
[]
quotes: "$@"
[]
quotes: "$AT" where AT="$@"
['']
no quotes: $AT
[]
HOW DOES THAT WORK

So i'm guessing there's a bit more setup than I'm realising…

@mpacer
Copy link

mpacer commented Feb 23, 2018

Ah… i just realised I needed to add args after the invocation:
edit:
$ ./test.sh has space arg 2

and I needed to group the args to get the effect you're seeing:
$ ./test.sh "has space" "arg 2"

I got:

./test.sh "has space" "arg 2"
$@ = has space arg 2
no quotes: $@
['has', 'space', 'arg', '2']
quotes: "$@"
['has space', 'arg 2']
quotes: "$AT" where AT="$@"
['has space arg 2']
no quotes: $AT
['has', 'space', 'arg', '2']
HOW DOES THAT WORK

So it looks like I can reproduce… this is so weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment