LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 05-18-2022, 06:01 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Quoting function calls in bash


Let me define a function as follows

Code:
time-stamp ()
 {
  if (( $# == 0)); then
    tmstamp="$(date +T%T.%N)"
  else
    tmstamp="$(date $1)"
  fi
  echo "$tmstamp"
 }
I can call the function using

Code:
stamp=$( time-stamp "+T%T.%N" )
Are there instances when one would quote the actual command with

Code:
stamp="$( ... )"
or

Code:
stamp='$( ... )'
 
Old 05-18-2022, 08:21 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Within " " $-expressions are evaluated, but it protects from further expansions.
You should use it in command arguments
Code:
echo "$( ... )"
You can use it in assignments
Code:
stamp="$( ... )"
but there is no effect, because in assignments there are no further expansions.

Within ' ' there are never any evaluations or expansions. (Unless you put an eval keyword in front.)
 
Old 05-19-2022, 02:56 AM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Complications arise when the call has arguments

Code:
stamp="$( date "$var" )"
 
Old 05-19-2022, 05:08 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by Faki View Post
Complications arise when the call has arguments

Code:
stamp="$( date "$var" )"
what complications?
Code:
stamp="$( date "${var:-+T%T.%N}" )"
 
Old 05-19-2022, 05:37 AM   #5
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Would there not be a problem having quotes inside quotes? I was trying to quote $var. Specifically, how would I pass a variable that includes a format with spaces?

Last edited by Faki; 05-19-2022 at 05:43 AM.
 
Old 05-19-2022, 06:04 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
why don't you try it yourself?
Bash recognizes $( ) and knows what is inside and outside. So it is not quotes inside quotes.
 
Old 05-19-2022, 06:29 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Within $( ) and ( ) there can be any shell code.
It is a "sub shell", forked from the main shell so it gets a copy of all the variables. When it terminates the main shell continues (variables from the sub shell are not copied back).

Last edited by MadeInGermany; 05-19-2022 at 01:29 PM.
 
1 members found this post helpful.
Old 05-19-2022, 08:07 AM   #8
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
I shall use

Code:
var=$(command "$opts")
 
Old 05-21-2022, 10:24 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Yes perfect; this is an assignment.

In a command you should have a $-expression in "quotes", otherwise the shell attempts further expansions:
Code:
echo "$( date "$var" )"
echo is a command in the main shell where $( ) should be in quotes,
date is a command in a sub shdell where $var should be in quotes.
 
  


Reply

Tags
bash, function, quoting



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
Quoting brackets inside a bash function sciss Linux - Newbie 11 09-30-2012 08:00 AM
[SOLVED] Bash syntax - question regarding quoting: quoting doesn't work as I expect. c.stadegaart Linux - General 5 04-02-2012 11:40 PM
a script calls another script which calls "bash" inside lordofring Linux - Software 10 03-18-2010 09:59 AM
shell quoting within a function dazdaz Linux - Software 4 09-03-2007 11:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:32 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
Open Source Consulting | Domain Registration