Using echo to print arguments inside a function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using echo to print arguments inside a function
# 1  
Old 01-03-2014
Using echo to print arguments inside a function

I am using echo in bash. Have created a function prargv which takes a number of arguments.

Example:

Code:
prargv "-e" "--examples"

Inside prargv, I want to print all the arguments using echo

Code:
echo "$@"

This returns

Code:
--examples

rather than

Code:
-e --examples"

This problem can be fixed by introducing a blank space

Code:
echo " $@"

How can I avoid echo interpreting -e when I do not put a blank at the beginning?
# 2  
Old 01-03-2014
Hi.

As seen in man echo, for some systems -e is treated as an option:
Code:
       -e     enable interpretation of backslash escapes

Using printf as in:
Code:
printf "%s\n" "-e --example"

produces:
Code:
-e --example

This is for:
Code:
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
printf - is a shell builtin [bash]

This also works with:
Code:
/usr/bin/printf "%s\n" "-e --example"

Best wishes ... cheers, drl
# 3  
Old 01-03-2014
That is correct, the -e is treated as an option. I found it strange that echo is interpreting it as I put $@ in double quotes.
# 4  
Old 01-03-2014
Hi.
Quote:
Originally Posted by kristinu
That is correct, the -e is treated as an option. I found it strange that echo is interpreting it as I put $@ in double quotes.
An illustration of $* and $@:
Code:
#!/usr/bin/env bash

echo
echo " asterisk"
echo "$*"

echo
echo " at sign"
echo "$@"

echo
echo " asterisk"
printf "%s\n" "$*"

echo
echo " at sign"
printf "%s\n" "$@"

producing:
Code:
$ ./s1 -e --example

 asterisk
-e --example

 at sign
--example

 asterisk
-e --example

 at sign
-e
--example

See man pages for details, and experiment.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call same function using 2 different arguments

I have a script that uses 2 arguments. I want to call the function part within this script using these same arguments. Below is what I came up with below script so far, any guidance would be helpful. Thank you! cat backup.sh #!/bin/bash function usage { echo "USAGE: $(basename $0)... (6 Replies)
Discussion started by: mbak
6 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. UNIX for Beginners Questions & Answers

How to avoid arguments inside Nawk command?

Hi, Here is my command print $2 was meant to select the second column however, it is getting substituted with the second argument that was passed to the script. Can you please tell me how can I resolve this ? (6 Replies)
Discussion started by: mohtashims
6 Replies

4. Shell Programming and Scripting

Need to call a function with arguments

I need to call a function within a code with $database and $ service as the arguments How do I proceed ? and how would a function be defined and these two arguments would be used inside the function? calc_pref_avail $database $service Best regards, Vishal (7 Replies)
Discussion started by: Vishal_dba
7 Replies

5. Shell Programming and Scripting

Unable to echo single quotes inside awk

# echo 'export HISTFILE=/var/log/history/history_$(uname -n)_$(date +%Y:%b:%d:%H:%M)_$(who am i | awk '{print \$1}')' >> new_file # # cat new_file export HISTFILE=/var/log/history/history_$(uname -n)_$(date +%Y:%b:%d:%H:%M)_$(who am i | awk {print $1}) # Now how to echo the quotes around the... (2 Replies)
Discussion started by: proactiveaditya
2 Replies

6. Shell Programming and Scripting

perform echo and awk inside a string

hi, just wanted to make a shortcut of this one a="a b c" b=`echo $a | awk '{print $2}'` echo "the middle is $b" why can't i do this: a="a b c" echo "the middle is ${`echo $a | awk '{print $2}'`}" <- bad substitution :wall: thanks (6 Replies)
Discussion started by: h0ujun
6 Replies

7. Shell Programming and Scripting

passing arguments to unix command or script inside tclsh

hi everobody kindly consider the following in tclsh I understand that we can do the following %exec UnixCmd arg1 arg2 but if I assinged the arguments to a list insde tclsh how can I use them back i.e %set ArgList %exec UnixCmd %exec Unixcmd $list %exec all the... (1 Reply)
Discussion started by: Blue_shadow
1 Replies

8. Shell Programming and Scripting

cat arguments to a function

Hi, I've a logging function in bourne shell, flog() which logs the first argument passed to it. How can I pass arguments to this function from a file, like cat filename | sed '...filtering...' | flog or cat filename | sed '...filtering...' | xargs flog Which did not work, after which... (3 Replies)
Discussion started by: Random_Net
3 Replies

9. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

10. Shell Programming and Scripting

Make new arguments for echo command

Hi everybody, i want to make an argument at echo command that takes a alpharithmetic and returns it reversed. How this can be done? plus what makefile changes are needed (0 Replies)
Discussion started by: Panteras
0 Replies
Login or Register to Ask a Question