How to pass string with several words and line endings \n as a parameter to a function
myFunc() { PARAM1=$1 echo “myFunc param1 = $PARAM1” } myvar=$(curl --help) echo "First test without quotes" myFunc $myvar echo "Second test with double quotes" myFunc "$myvar" echo "Third test with single quotes" myFunc '$myvar'
Solution : Second test
rst test : $myvar will be replaced by it’s value that contains a lot of words with spaces and line endings, for myFunc the first word will be the first parameter
Third test : $myvar is not interpreted
How display line endings \n that are inside a variable
myvar=$(curl --help) echo "First test (line endings have disappeared)" echo $myvar echo "Second test with correct display (line endings are present)" echo "$myvar"
line 3 line endings are notre interpreted
line 5 line endings are interpreted and you have the same display as if you execute the command curl –help
Leave a Reply