Arguments in variable vs direct string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arguments in variable vs direct string
# 1  
Old 11-09-2016
Question Arguments in variable vs direct string

Hello Community!
Let's say that we have some script which counts its arguments number:
arguments_count.sh:
Code:
#!/bin/sh
echo "Number of arguments="$#

and some test script:
test.sh:
Code:
#!/bin/sh
my_args="1 2 3 '4 5' 6"
echo "Count of arguments when using my_args:"
./arguments_count.sh $my_args
echo "Count of arguments when using direct string:"
./arguments_count.sh 1 2 3 '4 5' 6

Output after execute test.sh is:
Code:
Count of arguments when using my_args:
Number of arguments=6
Count of arguments when using direct string:
Number of arguments=5

What should I do to get output for "my_args" the same as for direct string?

Last edited by Scrutinizer; 11-09-2016 at 04:10 PM.. Reason: quote tags -> code tags
# 2  
Old 11-09-2016
Did you try double quoting $my_args?
# 3  
Old 11-09-2016
Quote:
Originally Posted by RudiC
Did you try double quoting $my_args?
Yes and the result is "1 argument".
# 4  
Old 11-09-2016
Yes I see; just found out myself. hmmm - I don't seem to achieve the desired result playing around... sorry.
# 5  
Old 11-09-2016
I wonder if not because comparing a variable with many values...What about comparing the same ?:
Code:
./arguments_count.sh $(echo 1 2 3 '4 5' 6)


Last edited by vbe; 11-09-2016 at 09:57 AM.. Reason: Typos, in my dyslexia period - sorry about that
# 6  
Old 11-09-2016
The problem is in the field splitting, after expansion of variable my_args. It is split into 6 fields:
1 2 3 '4 5' and 6
You see you called it my_args, but you turned it into a string instead of discrete arguments. By unquoting the string expansion you are not getting the arguments back, but 6 fields that stem from that string split on white space...

An alternative would be to use positional parameters:
Code:
set -- 1 2 3 '4 5' 6
echo "Count of arguments when using positional parameters"
./arguments_count.sh "$@"

--
If you are using bash, ksh93 or zsh you could try arrays:
Code:
my_args=(1 2 3 '4 5' 6)
echo "Count of arguments when using my_args array:"
./arguments_count.sh "${my_args[@]}"

Code:
Count of arguments when using positional parameters
Number of arguments=5
Count of arguments when using my_args array:
Number of arguments=5

Code:
echo "$4"; echo "${my_args[3]}"
4 5
4 5

# 7  
Old 11-09-2016
Scrutinizer is - as always - correct, but i suppose the problem comes from not understanding the quotation process:

The shell maintains a SINGLE flag for being inside a single- or double-quoted string. That means, that quotes can - unlike brackets - in no way be nested!

Consider:

Code:
(abc(def)ghi)
"abc"def"ghi"

These are completely different: there is not a string "def", which is part of a larger string "abc"def"ghi" (like the brackets-expression might be) but a string "abc", another string "ghi" and some unquoted "def" in between these two. So far so obvious and generally known.

But when it comes to single-quoted strings inside double-quoted strings some people think these are somehow nested:

Code:
"abc'def'ghi"

But this is not the case at all! In fact a single-quote character loses it special power to start or end a quotation inside a double.quoted string (and vice versa). That is, in the above example, the character between "c" and "d" is just an ordinary character, like "x" or any other.

If you want to create "nested quotes", which "peel off" one layer after the other you have to use escapes:

Code:
$ my_args="a b c \'d e\' f g"
$ arguments_count.sh $my_args

I hope this helps.

bakunin


I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

ksh parsing arguments in a string rather than from the cmdln

Hi. I have a piece of code that reads and parses command line options. I'd like to alter it slightly to read from a string (that's set elsewhere in the script) rather than directly from the command line (arg). Can somebody show me how to do this? Many thanks. My code is as follows: typeset... (6 Replies)
Discussion started by: user052009
6 Replies

3. Shell Programming and Scripting

Processing arguments in a string

Hi The following code works when reading the arguments from the command line but fails when I try to read from a string. So this works while ; do case $1 in -dbversion) if '`" ]; then { echo "ERROR: missing value for '$1' (seen '$2')"; usage; exit 1; } else { shift;... (6 Replies)
Discussion started by: user052009
6 Replies

4. Shell Programming and Scripting

Manipulating sed Direct Input to Direct Output

Hi guys, been scratching round the forums and my mountain of resources. Maybe I havn't read deep enough My question is not how sed edits a stream and outputs it to a file, rather something like this below: I have a .txt with some text in it :rolleyes: abc:123:xyz 123:abc:987... (7 Replies)
Discussion started by: the0nion
7 Replies

5. Shell Programming and Scripting

Removing command line arguments from string list

I am passing a list of strings $list and want to remove all entries with --shift=number, --sort=number/number/..., --group=number/number/... Also are removed whether upper or lower case letters are used For example the following will all be deleted from the list --shift=12 --shift=2324... (7 Replies)
Discussion started by: kristinu
7 Replies

6. Shell Programming and Scripting

string manipulation in arguments

Hi all, I have a requirement where I am taking the first argument as argument name and storing the second argument in argument name as value. Thanks to ppl here, i learnt to do it.:p while ( $1 != "" ) set arg = $1 shift set val = "$1" echo "set... (2 Replies)
Discussion started by: animesharma
2 Replies

7. Shell Programming and Scripting

Print arguments with the help of variable

Let's say I want to print the arguments $4 till $#, how can I do this? $# contains the number of arguments $@ contain all the arguments as string What i need is something like for i in $4_till_$#; do #do something with $i convert $i ~/$output done The first 3 arguments are used as options... (6 Replies)
Discussion started by: hakermania
6 Replies

8. Shell Programming and Scripting

How to make bash wrapper for java/groovy program with variable length arguments lists?

The following bash script does not work because the java/groovy code always thinks there are four arguments even if there are only 1 or 2. As you can see from my hideous backslashes, I am using cygwin bash on windows. export... (1 Reply)
Discussion started by: siegfried
1 Replies

9. Shell Programming and Scripting

How to call arguments with variable in a script??

Hello, I was wondering if it were possible to call arguments passed to a script using a variable. For example: sh script.sh yes no good bad x=$# while do echo (last argument, then second last etc until first argument) let x=($x-1) done should print out bad good no (4 Replies)
Discussion started by: VanK
4 Replies

10. Shell Programming and Scripting

Joining string arguments

Hi, how can I join given arguments (not starting from the first one) to form one string, each argument separated by a space. For example, out of 5 given arguments, I'll like to start joining from the 3rd to the last. In python there exists something like ' '.join(sys.argv) and it starts joining... (5 Replies)
Discussion started by: plhelpme
5 Replies
Login or Register to Ask a Question