LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 09-01-2007, 07:00 PM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL 6.x, 5.x, Fedora 20, Kubuntu 12.04, Solaris 10,8
Posts: 321

Rep: Reputation: 17
shell quoting within a function


Hi all,

So here's a nice brain teaser. I am trying to write a shell function that will allow me to view files in directories with spaces or special characters. I am using eval to pad this out.

It works fine with a shell alias, but not a shell function.

I've tried so many variations, I need some guidance. Please help :-)

$ mkdir abc\ xyz

$ set -xv
$ alias ll='ls -l `eval $@`'
alias ll='ls -l `eval $@`'
+ alias 'll=ls -l `eval $@`'
history -a
++ history -a
$ ll abc\ xyz/
ll abc\ xyz/
eval $@
++ eval
+ ls -l 'abc xyz/'
total 0
-rw-r--r-- 1 root root 0 2007-09-02 00:53 testfile
history -a
++ history -a



$ function lll () { ls -l `eval $@`; }
function lll () { ls -l `eval $@`; }
history -a
++ history -a

$ lll abc\ xyz/
lll abc\ xyz/
+ lll 'abc xyz/'
eval $@
++ eval abc xyz/
abc xyz/
+++ abc xyz/
bash: abc: command not found
+ ls -l
total 4944
-rw-r--r-- 1 root root 3837738 2007-04-10 23:36 allpackages.html
-rw-r--r-- 1 root root 1036131 2007-04-10 21:04 debian4.0-r0-etch-dvd1
-rw-r--r-- 1 root root 90937 2007-06-23 22:08 report_all_plugins.html
history -a
++ history -a
 
Old 09-02-2007, 02:56 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,944
Blog Entries: 54

Rep: Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731
Why would you want to "read ARGs as input to the shell and execute the resulting command" (aka 'eval') things when you use a default IFS and have already escaped space?
 
Old 09-02-2007, 03:00 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by dazdaz View Post
It works fine with a shell alias, but not a shell function.
Hi. I'd tell the opposite. What you expected is done by the function not by the alias: it is the logic not quite correct. Using eval means execute its arguments as a command. If you do something like
Code:
eval somefile somedir
the shell tries to execute the command
Code:
somefile somedir
that is obviously not correct. Regarding the alias and the function, in case of the alias you obtain
Code:
$ ll abc\ xyz/
ll abc\ xyz/
eval $@
++ eval
+ ls -l 'abc xyz/'
Here eval evaluates nothing, because $@ are the arguments (better, the positional parameters) to the ls -l command, not to the eval subprocess. Evaluating a null string means to execute nothing, therefore no error is given. On the contrary in the function you obtain
Code:
$ lll abc\ xyz/
lll abc\ xyz/
+ lll 'abc xyz/'
eval $@
++ eval abc xyz/
abc xyz/
+++ abc xyz/
bash: abc: command not found
+ ls -l
Here the positional parameters are the arguments to the function and they are evaluated in any position (statement) inside the function itself. The eval command (in blue) evaluates the string "abc xyz" and try to execute it as a command, therefore the "command not found error". Then ls -l (in dark red) is executed with no arguments at all, because of the previous error.
 
Old 09-02-2007, 04:15 AM   #4
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL 6.x, 5.x, Fedora 20, Kubuntu 12.04, Solaris 10,8
Posts: 321

Original Poster
Rep: Reputation: 17
It makes more sense now.

function lll () { ls -l "$@"; }

Thanks.
 
Old 09-03-2007, 11:01 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
The key is quoting the $@, this causes the positional parameters (arguments) to seen as the separate words they were in the command line.

See man bash & do a search for 'Special Parameters'.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 05:54 PM
arithmethic function inside shell script mystical dervish Linux - Newbie 3 08-21-2007 04:57 AM
bash - quoting RGummi Linux - General 3 10-21-2006 03:06 PM
Quick question: quoting text in sed overbored Linux - Software 0 06-24-2004 01:23 PM
Shell function syntax error... psyklops Programming 1 09-03-2003 01:55 AM


All times are GMT -5. The time now is 09:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration