User Tools

Site Tools



This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
action:Execute_a_Shell_Script [2019/11/25 16:53]
action:Execute_a_Shell_Script [2020/07/08 22:43] (current)
peternlewis Handling multiple arguments within a single variable
Line 210: Line 210:
 * `")` - single quoted string * `")` - single quoted string
 +For the reverse problem, where you have a variable that contains multiple parameters, note that you cannot use quotes within a variable - by the time the shell is expanding variables, it has already processed quotes and will not do so again so quotes will just be regular characters passed to the target command and almost certainly result in errors. If possible, use seperate variables for seperate parameters, but if you cannot do that (for example you have a list of paths), store them as seperate lines in a variable (without any quoting or backslashes) and use a command like:
 +echo "​$KMVAR_files"​ | tr '​\r\n'​ '​\0'​ | xargs -0 ls -l
 +The tr command will replace `\r` or `\n` line endings with a nul character, and the `xargs -0` command will read that, split the arguments at the nul character, and pass them to the specified command (in this case `ls -l`. Note that xargs has a limit to the number of arguments it will pass, so for large numbers of arguments it may run the command multiple times with subsets of the arguments - read the `xargs` man page for more details.
 ===== Path in Shell Scripts ===== ===== Path in Shell Scripts =====
action/Execute_a_Shell_Script.txt ยท Last modified: 2020/07/08 22:43 by peternlewis