Skip to content

Instantly share code, notes, and snippets.

@gevasiliou
Created February 22, 2017 14:20
Show Gist options
  • Save gevasiliou/abe6c8d84924e9ecfb6930ebef9f8fbe to your computer and use it in GitHub Desktop.
Save gevasiliou/abe6c8d84924e9ecfb6930ebef9f8fbe to your computer and use it in GitHub Desktop.
#!/bin/bash
#In case we want to work with args that may contain any character, one could think to separate args by null byte \0
function test {
while IFS= read -r -d '' f;do echo "f= $f";done< <(echo "$@" |base64 -d)
}
arg1="some more"
arg2="text here"
args=$(echo -e "$arg1\0$arg2\0" |base64)
#We need base64 or xxd since bash removes null by default. Converting to base64 we preserve the null byte.
test "$args"
#Output:
f= some more
f= text here
#Can even work directly without function:
#while IFS= read -r -d '' f;do echo "f= $f";done< <(echo "$args" |base64 -d) #base64 variable is decoded
#----------------------------------------------------------------------------------------------------------#
#another test
function test2 {
for arg in $@;do
echo "test2-arg=" $(echo "$arg" |base64 -d)
done
}
arg11=$(echo -e "$arg1\0" |base64)
arg22=$(echo -e "$arg2\0" |base64)
test2 $arg11 $arg22
#Output:
#test2-arg= some more
#test2-arg= text here
#-----------------------------------------------------------------------------------------------------------#
#On recent bash 4.4 readarray or mapfile who accept a delimiter (-d option) can also work:
#IFS= mapfile -t -d '' arr < <(echo "$args" |base64 -d) #base64 variable decoded
#declare -p arr #should contain both args
#----------------------------------------------------------------------------------------------------------#
#For verification if your vars contain null byte you can pipe your echo/cat/whatever to |od -t x1c
#If null is there will be reported as 00 , \0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment