How do I learn how to get quoting right in bash?

后端 未结 3 649
不思量自难忘°
不思量自难忘° 2021-01-06 16:40

I\'m constantly confused by the rules for quoting and evaluating when I\'m writing bash scripts. I know some of the basics, like the difference between \'\' and \"\" and ``,

3条回答
  •  情深已故
    2021-01-06 17:17

    At the shell prompt,

    set -x
    set -v
    

    and create the following python program 'args.py'

    #!/usr/bin/env python
    
    import sys
    print sys.argv
    for arg in sys.argv[1:]:
        for c in arg:
            print c,"|",
        print
    

    Then experiment with command invocations like:

    U=hello\ world ; V="-->$U<--"; W="1 $U 2 $V 3"; args.py $W
    

    Until you realize that there is no logical way of thinking about what is going on. It really is all done by capricious magic shell pixies.

提交回复
热议问题