Skip to content

Instantly share code, notes, and snippets.

@eqvinox
Created July 9, 2022 15:27
Show Gist options
  • Select an option

  • Save eqvinox/0b8ff0ad5fd7e4667d0888ba226fca9e to your computer and use it in GitHub Desktop.

Select an option

Save eqvinox/0b8ff0ad5fd7e4667d0888ba226fca9e to your computer and use it in GitHub Desktop.
POSIX shell escaping demo-sheet
#!/bin/sh
# shell escaping "demo-sheet"
# "args" just prints argv[]
filename="/tmp/some file"
echo 'foo echo some file contents' > "$filename"
set -x
args $(echo foo echo wrong)
# + echo foo echo wrong
# + args foo echo wrong
# [1]: "foo"
# [2]: "echo"
# [3]: "wrong"
args "$(echo foo echo better)"
# + echo foo echo better
# + args foo echo better
# [1]: "foo echo better"
args $(cat $filename)
# + cat /tmp/some file
# cat: /tmp/some: No such file or directory
# cat: file: No such file or directory
# + args
args $(cat "$filename")
# + cat /tmp/some file
# + args foo echo some file contents
# [1]: "foo"
# [2]: "echo"
# [3]: "some"
# [4]: "file"
# [5]: "contents"
args "$(cat "$filename")"
# + cat /tmp/some file
# + args foo echo some file contents
# [1]: "foo echo some file contents"
args "$(cat \"$filename\")"
# + cat "/tmp/some file"
# cat: '"/tmp/some': No such file or directory
# cat: 'file"': No such file or directory
# + args
# [1]: ""
args `cat $filename`
# + cat /tmp/some file
# cat: /tmp/some: No such file or directory
# cat: file: No such file or directory
# + args
args `cat "$filename"`
# + cat /tmp/some file
# + args foo echo some file contents
# [1]: "foo"
# [2]: "echo"
# [3]: "some"
# [4]: "file"
# [5]: "contents"
args "`cat "$filename"`"
# + cat /tmp/some file
# + args foo echo some file contents
# [1]: "foo echo some file contents"
args "`cat \"$filename\"`"
# + cat /tmp/some file
# + args foo echo some file contents
# [1]: "foo echo some file contents"
rm "$filename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment