bash error on eval containing blank space

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I need to triggeer a bash passing parameter with a format [ variable_name="value" ] like this:



./test.sh ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano'


On test.sh script I want to assign to any variable the relevant value:



#!/bin/bash
eval `echo "$@"`
echo $ftpcmd


But I get a prova: command not found error.



I cannot understand what's wrong on my ftpcmd='CWD debug' parameter neither how I should write it instead.



If I try to replace eval with declare then the $ftpcmd is set only to CWD instead of CWD debug as I'm expecting.










share|improve this question



















  • 2




    don't reinvent the wheel. option parsing was done years ago. use getopts (bash built-in, short single-letter options only) or /usr/bin/getopt (from util-linux, supports both short and --long options)
    – cas
    Oct 2 '15 at 10:39







  • 1




    see: unix.stackexchange.com/questions/62950/…
    – cas
    Oct 2 '15 at 10:44










  • @cas you're right. As I wrote I've still many things to learn on bash scripting.
    – Stefano Radaelli
    Oct 2 '15 at 10:57














up vote
1
down vote

favorite












I need to triggeer a bash passing parameter with a format [ variable_name="value" ] like this:



./test.sh ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano'


On test.sh script I want to assign to any variable the relevant value:



#!/bin/bash
eval `echo "$@"`
echo $ftpcmd


But I get a prova: command not found error.



I cannot understand what's wrong on my ftpcmd='CWD debug' parameter neither how I should write it instead.



If I try to replace eval with declare then the $ftpcmd is set only to CWD instead of CWD debug as I'm expecting.










share|improve this question



















  • 2




    don't reinvent the wheel. option parsing was done years ago. use getopts (bash built-in, short single-letter options only) or /usr/bin/getopt (from util-linux, supports both short and --long options)
    – cas
    Oct 2 '15 at 10:39







  • 1




    see: unix.stackexchange.com/questions/62950/…
    – cas
    Oct 2 '15 at 10:44










  • @cas you're right. As I wrote I've still many things to learn on bash scripting.
    – Stefano Radaelli
    Oct 2 '15 at 10:57












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I need to triggeer a bash passing parameter with a format [ variable_name="value" ] like this:



./test.sh ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano'


On test.sh script I want to assign to any variable the relevant value:



#!/bin/bash
eval `echo "$@"`
echo $ftpcmd


But I get a prova: command not found error.



I cannot understand what's wrong on my ftpcmd='CWD debug' parameter neither how I should write it instead.



If I try to replace eval with declare then the $ftpcmd is set only to CWD instead of CWD debug as I'm expecting.










share|improve this question















I need to triggeer a bash passing parameter with a format [ variable_name="value" ] like this:



./test.sh ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano'


On test.sh script I want to assign to any variable the relevant value:



#!/bin/bash
eval `echo "$@"`
echo $ftpcmd


But I get a prova: command not found error.



I cannot understand what's wrong on my ftpcmd='CWD debug' parameter neither how I should write it instead.



If I try to replace eval with declare then the $ftpcmd is set only to CWD instead of CWD debug as I'm expecting.







bash shell quoting variable whitespace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 21 at 3:15









Rui F Ribeiro

36.7k1271116




36.7k1271116










asked Oct 2 '15 at 10:37









Stefano Radaelli

1084




1084







  • 2




    don't reinvent the wheel. option parsing was done years ago. use getopts (bash built-in, short single-letter options only) or /usr/bin/getopt (from util-linux, supports both short and --long options)
    – cas
    Oct 2 '15 at 10:39







  • 1




    see: unix.stackexchange.com/questions/62950/…
    – cas
    Oct 2 '15 at 10:44










  • @cas you're right. As I wrote I've still many things to learn on bash scripting.
    – Stefano Radaelli
    Oct 2 '15 at 10:57












  • 2




    don't reinvent the wheel. option parsing was done years ago. use getopts (bash built-in, short single-letter options only) or /usr/bin/getopt (from util-linux, supports both short and --long options)
    – cas
    Oct 2 '15 at 10:39







  • 1




    see: unix.stackexchange.com/questions/62950/…
    – cas
    Oct 2 '15 at 10:44










  • @cas you're right. As I wrote I've still many things to learn on bash scripting.
    – Stefano Radaelli
    Oct 2 '15 at 10:57







2




2




don't reinvent the wheel. option parsing was done years ago. use getopts (bash built-in, short single-letter options only) or /usr/bin/getopt (from util-linux, supports both short and --long options)
– cas
Oct 2 '15 at 10:39





don't reinvent the wheel. option parsing was done years ago. use getopts (bash built-in, short single-letter options only) or /usr/bin/getopt (from util-linux, supports both short and --long options)
– cas
Oct 2 '15 at 10:39





1




1




see: unix.stackexchange.com/questions/62950/…
– cas
Oct 2 '15 at 10:44




see: unix.stackexchange.com/questions/62950/…
– cas
Oct 2 '15 at 10:44












@cas you're right. As I wrote I've still many things to learn on bash scripting.
– Stefano Radaelli
Oct 2 '15 at 10:57




@cas you're right. As I wrote I've still many things to learn on bash scripting.
– Stefano Radaelli
Oct 2 '15 at 10:57










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










If you put the assignments before the script



ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano' ./test.sh 


the variables are available in the script's environment. The -k option treats all assignments, not just pre-command assignments, as environment modifications.



set -k
./test.sh ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano'
set +k


In either case, eval is not required at all and can be removed.






share|improve this answer




















  • In such case your suggestion is to have to to a parent shell that runs test.sh. Am I right?
    – Stefano Radaelli
    Oct 2 '15 at 15:50










  • For the first example (x=1 y="abc" ./cmd.sh), no, you are simply reordering the command.
    – michael
    Oct 3 '15 at 13:21










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f233467%2fbash-error-on-eval-containing-blank-space%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










If you put the assignments before the script



ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano' ./test.sh 


the variables are available in the script's environment. The -k option treats all assignments, not just pre-command assignments, as environment modifications.



set -k
./test.sh ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano'
set +k


In either case, eval is not required at all and can be removed.






share|improve this answer




















  • In such case your suggestion is to have to to a parent shell that runs test.sh. Am I right?
    – Stefano Radaelli
    Oct 2 '15 at 15:50










  • For the first example (x=1 y="abc" ./cmd.sh), no, you are simply reordering the command.
    – michael
    Oct 3 '15 at 13:21














up vote
2
down vote



accepted










If you put the assignments before the script



ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano' ./test.sh 


the variables are available in the script's environment. The -k option treats all assignments, not just pre-command assignments, as environment modifications.



set -k
./test.sh ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano'
set +k


In either case, eval is not required at all and can be removed.






share|improve this answer




















  • In such case your suggestion is to have to to a parent shell that runs test.sh. Am I right?
    – Stefano Radaelli
    Oct 2 '15 at 15:50










  • For the first example (x=1 y="abc" ./cmd.sh), no, you are simply reordering the command.
    – michael
    Oct 3 '15 at 13:21












up vote
2
down vote



accepted







up vote
2
down vote



accepted






If you put the assignments before the script



ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano' ./test.sh 


the variables are available in the script's environment. The -k option treats all assignments, not just pre-command assignments, as environment modifications.



set -k
./test.sh ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano'
set +k


In either case, eval is not required at all and can be removed.






share|improve this answer












If you put the assignments before the script



ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano' ./test.sh 


the variables are available in the script's environment. The -k option treats all assignments, not just pre-command assignments, as environment modifications.



set -k
./test.sh ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano'
set +k


In either case, eval is not required at all and can be removed.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 2 '15 at 15:14









chepner

5,2001223




5,2001223











  • In such case your suggestion is to have to to a parent shell that runs test.sh. Am I right?
    – Stefano Radaelli
    Oct 2 '15 at 15:50










  • For the first example (x=1 y="abc" ./cmd.sh), no, you are simply reordering the command.
    – michael
    Oct 3 '15 at 13:21
















  • In such case your suggestion is to have to to a parent shell that runs test.sh. Am I right?
    – Stefano Radaelli
    Oct 2 '15 at 15:50










  • For the first example (x=1 y="abc" ./cmd.sh), no, you are simply reordering the command.
    – michael
    Oct 3 '15 at 13:21















In such case your suggestion is to have to to a parent shell that runs test.sh. Am I right?
– Stefano Radaelli
Oct 2 '15 at 15:50




In such case your suggestion is to have to to a parent shell that runs test.sh. Am I right?
– Stefano Radaelli
Oct 2 '15 at 15:50












For the first example (x=1 y="abc" ./cmd.sh), no, you are simply reordering the command.
– michael
Oct 3 '15 at 13:21




For the first example (x=1 y="abc" ./cmd.sh), no, you are simply reordering the command.
– michael
Oct 3 '15 at 13:21

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f233467%2fbash-error-on-eval-containing-blank-space%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)