LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-10-2013, 02:24 PM   #1
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 203

Rep: Reputation: 27
Meaning of parameters in bash function: eval, $@, $#


The following function is used to create a directory and cd into it. It must be added to the .bashrc file inside your home directory.
Code:
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\"";
}
I am having trouble understanding what this
Code:
"$@" && eval cd "\"\$$#\""
is used for.
I know that
-$@: All arguments as separate words.
-$#: Number of arguments.
And I believe that the backward slashes (\) are used as escape sentences, but I'm not really sure.
Any comments will be more than welcome.
 
Old 04-10-2013, 04:35 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
The first command,
Code:
mkdir -p "$@"
creates directories specified by the arguments supplied to the function. The "$@" ensures that names containing spaces will be treated correctly. The other command,
Code:
eval cd "\"\$$#\""
cd's to the last directory specified. The entire point here is to get the last argument of the function. The example uses eval to make the command be evaluated twice. So, during the first pass \" will become ", \$ will become $ and $# will be replaced by its value. Then the result will be executed as a command.
So, asuming that the function is called as, for example:

Code:
mkdircd a b c d
Directories a, b, c and d will be created. Then, eval will force the other part to be evaluated. So,
Code:
cd "\"\$$#\""
will become
Code:
cd "$4"
because the function was called with 4 arguments.
However, keep in mind that eval is a tool that is best left alone.
Instead, I would write
Code:
mkdircd () { mkdir -p "$@" && cd "${!#}"; }
which IMHO is much nicer.

Also, please note that a function definition in bash is either
Code:
function foo { ... }
or
Code:
foo() { ... }
Using of
Code:
function foo() { ... }
is not recommended.
 
2 members found this post helpful.
Old 04-10-2013, 07:19 PM   #3
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 203

Original Poster
Rep: Reputation: 27
Thanks a lot! For taking the time to write and also for sharing your knowledge.
I added to your reputation and will mark this thread as solved.
Just one more thing. Would you mind very much explaining why this
Code:
function foo() { ... }
is not recommended?
 
Old 04-10-2013, 08:33 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
See pt 25 here http://mywiki.wooledge.org/BashPitfa...tion_foo.28.29
 
Old 04-10-2013, 09:39 PM   #5
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 203

Original Poster
Rep: Reputation: 27
Quote:
Originally Posted by chrism01 View Post
Thanks a lot chrism01! I will add that page to my bookmarks.
 
Old 04-11-2013, 04:34 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Here are two other ways to get the directory name:

Code:
mkdircd() { mkdir -p "$@" && cd "${@: -1}" ;}    #or "${@:(-1)}"
Since "$@" is a kind of array, you can use bash's array expansion patterns with it. Either a space or parentheses must be included when using negative values, to avoid conflict with the default value parameter substitution.

Code:
mkdircd() { mkdir -p "$@" && cd "$_" ;}
"$_" expands to the last argument of the most recent command run.
 
1 members found this post helpful.
Old 04-11-2013, 05:56 PM   #7
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 203

Original Poster
Rep: Reputation: 27
Quote:
Originally Posted by David the H. View Post
Here are two other ways to get the directory name:

Code:
mkdircd() { mkdir -p "$@" && cd "${@: -1}" ;}    #or "${@:(-1)}"
Since "$@" is a kind of array, you can use bash's array expansion patterns with it. Either a space or parentheses must be included when using negative values, to avoid conflict with the default value parameter substitution.

Code:
mkdircd() { mkdir -p "$@" && cd "$_" ;}
"$_" expands to the last argument of the most recent command run.
Thank you David the H!
 
  


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
Passing parameters to bash script function (or subroutine) withanh Linux - Newbie 5 02-19-2012 08:54 AM
[SOLVED] bash how pass array as one of several parameters to a function porphyry5 Programming 5 06-14-2011 09:11 PM
[SOLVED] Writing a function, question about a symbol's meaning Alex_Dc Linux - Newbie 3 10-14-2009 10:25 PM
bash function parameters ArthurHuang Programming 1 05-18-2009 06:01 PM
PHP eval() function nxny Programming 2 01-09-2003 02:39 PM


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration