Advertisement
Guest User

Untitled

a guest
May 29th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. # Study: Bash Variable Quoting
  2.  
  3. Bash is designed to automatically reinterpret where strings end and begin based on whitespace unless the string is
  4. surrounded by double quotes. It's because Bash is so string-centric that you can call a script or function like this:
  5. ```bash
  6. some_func John Doe "age 24"
  7. ```
  8. and all of those argument will be interpreted as string literal, not variable name or command names. If you omit the
  9. quotes around "age 24" the arg count will increase to four.
  10.  
  11. Inside the function you have a similar situation. Even if the last arg is passed in with double quotes you can run into
  12. this:
  13. ```bash
  14. printf $1 #-> John
  15. printf $2 #-> Doe
  16. printf $3 #-> Age
  17. ```
  18. This is called __word splitting__, the `24` is actually still sent to `printf`, but it's send as second (discarded)
  19. arguments. To put it more accurately, each function/script gets it's arguments as an array list of words where each
  20. element can only be more than one word if it is wrapped in double quotes.
  21.  
  22. __Parameter expansion__ is the procedure to get the value from the referenced entity, like expanding a variable to
  23. print its value. This is when $3 is expanded into an array, using whitespace as delimitors. You can think
  24. of double quotes in bash as a shell protecting whatever is inside from parameter expansion.
  25.  
  26. Inside of a script or function you have `"$@"`, which holds all the arguments passed in as a list. If we execute:
  27.  
  28. ```bash
  29. some_func John Doe "age 24"
  30. ```
  31.  
  32. `"$@"`, inside the function, basically contains an array which would be declared and used like this:
  33.  
  34. ```bash
  35. array=("John" "Doe" "age 24")
  36.  
  37. echo "${array[0]}"
  38. echo "${array[1]}"
  39. echo "${array[2]}"
  40. ```
  41. The the quotes around "John" and "Doe" aren't really need but they are put there for consistency. The
  42. space between them I interpreted by `(` and `)` to make a new index, not the `"` symbol. In fact,
  43. try this `array=("John""Doe""age 24")` and you will get:
  44. ```bash
  45. John
  46. Doeage 24
  47. ```
  48.  
  49. The `"` are discarded by when passed into functions too. You might not believe it, but all of these work:
  50.  
  51. ```bash
  52. dirname /User/Jeff
  53. dirname "/Us"er"/Jeff"
  54. ```
  55. What doesn't work is if you have a path that contains a space and is not surrounded by double quotes.
  56.  
  57. ```bash
  58. dirname "Users/Jeff/a dir/the index.html" # works
  59. dirname Users/Jeff/a" "dir/the" "index.html # works
  60. dirname "Users/Jeff/a" "dir/the index.html" # fails
  61. ```
  62. As long as the quotes are open when we hit a space, it works. The most bizzare aspect of Bash is that
  63. `"` is ignored when parsing a script or function name, all of these work:
  64. ```bash
  65. echo $(dirn"am"e "/Us"er"/Jeff")
  66.  
  67. it_works () { echo "it works"; }
  68.  
  69. echo `it_"works"`
  70. echo $("it_"works)
  71. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement