[Discussion] POSIX, the Love of Backticks and All That Jazz


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users [Discussion] POSIX, the Love of Backticks and All That Jazz
# 8  
Old 01-27-2017
Quote:
Originally Posted by Peasant
If i'm not mistaken backquotes are still the lowest common denominator for command substitution if you wish your scripts to work anywhere without touching
This is an interesting argument.

IMHO the "gold standard" of portability is POSIX: if something is POSIX, then it can (and should) be used, if it not, it should be handled with extreme care (and eventually not be used in absence of compelling reasons why it should).

The construct $(..) now is in fact the POSIX-standard for doing this (subshells) and backticks `...` are deprecated. This is why i use the former rather than the latter and recommend this practice to others.

For the same reason i write i.e. tail -n 5 instead of tail -5. The former is POSIX, the latter deprecated. It may be that there is some (really old) system out there which would not recognise the new syntax but only the old one. This risk i am willing to take because, on the the other hand, there might be a really new system which has cut off old behaviorisms and only understands the current syntax.

I hope this helps.

bakunin

Moderator's Comments:
Mod Comment PS: this discussion is very interesting but we are getting off topic in relation the threads original theme. If anyone wants to continue this i suggest to split this thread and carry on in a new one. Just post here if you want to contribute and i will take care of the splitting if there are any takers.
# 9  
Old 01-27-2017
Quote:
Originally Posted by MadeInGermany
Then let me continue with lession#2.
Within $( ) there is a parser problem with the old style case statements.
Code:
#!/bin/bash
res=$(
  case $1 in
  1) echo 1;;
  esac
)
# syntax error in bash3 and ksh88
# workaround:
# (1) echo 1;;
echo "$res"

But bash4 and ksh93 have got a fix.
Bash is not standard on AIX. And many people I see using bash are using bash-4.2 not 4.4 - so when you say bash4 - do you mean any bash-4.X or are you talking about the X (again, most people just say bash - not bashX).

So, maybe - we should split this off - but please come up with a catchy "subject line" - as we are sort of "all over the place" - shells, POSIX, environments, ...
# 10  
Old 01-28-2017
But backquotes are not depreciated if i'm reading the right standards !

Code:
2.6.3 Command Substitution

Command substitution allows the output of a command to be substituted in place of the command name itself. Command substitution shall occur when the command is enclosed as follows:

$(command)

or (backquoted version):

`command`

It's usage might be depreciated in modern shells as a good practice.
Bash or ksh manual on debian testing still shows backquotes as valid syntax.

It does not mention not to use it, or to prefer $(command) syntax.

Regards
Peasant.
# 11  
Old 01-28-2017
Hi Peasant,
You are correct in saying that back-quotes are not deprecated as a method of performing command substitution in the standards. And, even though some people would like to, it is unlikely that the standards will formally deprecate back-quoted command substitution because so many existing (otherwise strictly conforming) shell scripts use them.

But, when writing new scripts (unless you intend to have them run on a pure Bourne shell that doesn't have $(command) command substitution), I would always suggest using the $(command) form... Note that:
  1. $(command) command substitutions can be nested,
  2. `command` command substitutions cannot be nested, and
  3. if you need to quote the results of a command substitution where the command contains a double-quoted string, the double quotes inside a $(command) command substitution are not affected by double quotes outside the command substitution; but double quotes inside a `command` command substitution produce undefined results if the command substitution is inside double quotes.
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 01-28-2017
Quote:
Originally Posted by Don Cragun
But, when writing new scripts (unless you intend to have them run on a pure Bourne shell that doesn't have $(command) command substitution), I would always suggest using the $(command) form... Note that:
  1. $(command) command substitutions can be nested,
  2. `command` command substitutions cannot be nested, and
  3. if you need to quote the results of a command substitution where the command contains a double-quoted string, the double quotes inside a $(command) command substitution are not affected by double quotes outside the command substitution; but double quotes inside a `command` command substitution produce undefined results if the command substitution is inside double quotes.
I came across something like this in the early days of AudioScope and SOX.
I just assumed that it was me not coding correctly.
Thanks for the info.
# 13  
Old 01-28-2017
Quote:
Originally Posted by Don Cragun
Hi Peasant,
You are correct in saying that back-quotes are not deprecated as a method of performing command substitution in the standards. And, even though some people would like to, it is unlikely that the standards will formally deprecate back-quoted command substitution because so many existing (otherwise strictly conforming) shell scripts use them.

But, when writing new scripts (unless you intend to have them run on a pure Bourne shell that doesn't have $(command) command substitution), I would always suggest using the $(command) form... Note that:
  1. $(command) command substitutions can be nested,
  2. `command` command substitutions cannot be nested, and
  3. if you need to quote the results of a command substitution where the command contains a double-quoted string, the double quotes inside a $(command) command substitution are not affected by double quotes outside the command substitution; but double quotes inside a `command` command substitution produce undefined results if the command substitution is inside double quotes.
Hi Don, `command` command substitutions can be nested, but the inner backquotes need to be escaped..
Code:
$ echo "$(echo "foo  $(echo "bar  baz")")" 
foo  bar  baz
$ echo "`echo \"foo  \`echo \\\"bar  baz\\\"\`\"`" 
foo  bar  baz

Also, IMO double quotes inside a `command` command substitution do not produce undefined results; they need to be properly escaped in accordance to the nesting level, which quickly leads to an escape hell, but it is not impossible. Why anyone would prefer backticks over $( ... ) is beyond me though..

Last edited by Scrutinizer; 01-28-2017 at 11:26 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 01-28-2017
Quote:
Originally Posted by MichaelFelt
So, maybe - we should split this off - but please come up with a catchy "subject line"
So done. Catchy enough for you? ;-))

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nesting backticks

I'm trying to make a dialog window that prints the output of grep that takes the output of find. Unfortunately my nested backticks don't work. Here is the dialog window: dialog --stdout --title "test" --backtitle "test" --msgbox "Test:\n `grep -l "${tablica}" `find $string``" 16 60I think I... (2 Replies)
Discussion started by: Starting_Leaf
2 Replies

2. Shell Programming and Scripting

SSH and Backticks [solved]

I have been testing a new script and cannot figure out why my `cat spath` will not execute on the remote machine? sudo ssh -p 22344 -o "PasswordAuthentication no" -o "HostbasedAuthentication yes" -l testuser 192.168.1.6 "find `cat spath` -depth" cat: spath: No such file or directory but... (0 Replies)
Discussion started by: metallica1973
0 Replies

3. Shell Programming and Scripting

Help with remove backticks in a text file

Input file: 'data_1' 'data_10' 'data1311' '235data_13' Desired output: data_1 data_10 data1311 235data_13 Can I know how to remove back tick"'" in a file? Many thanks for advice. (3 Replies)
Discussion started by: perl_beginner
3 Replies

4. Red Hat

perl backticks: can't redirect output.

Hi everyone. This is a bit of a perl/linux mixed question. I am trying to redirect STDOUT of chsh by using the following line of perl code. system ("chsh -s /sbin/nologin $testing 1>/dev/null"); This should redirect STDOUT to /dev/null but it won't do that for some odd reason. Any ideas or... (6 Replies)
Discussion started by: austinharris43
6 Replies

5. Shell Programming and Scripting

Difference between using xargs and backticks

Hey all. Just a fast question, what is the technical difference between using back ticks and using xargs to perform a command? Here's an example Find /mydir -name *.conf |xargs rm Vs Rm 'find /mydir -name *.conf' Is there a performance hit? I know they do the same thing but which is... (1 Reply)
Discussion started by: msarro
1 Replies

6. Shell Programming and Scripting

Am I abusing backticks?

I'm always concerned I might be abusing backticks within my scripts. A current script I'm writing has this for example: stripscriptname=`echo $scriptname | sed 's/\(.*\)\..*/\1/'` stripsearch=`echo $searchpattern | tr -d ' ,/'` Both of these variables are set inside the script (in fact,... (2 Replies)
Discussion started by: mglenney
2 Replies

7. UNIX for Dummies Questions & Answers

sed: removing backticks from certain lines

Hi, I would like to change some lines in my mysql-dump, because there a syntax problems with some version of mysql. I 'd like to change USE ´someDatabase´; to USE someDatabase; (without backticks) using the sed command in the shell Thanks & best regards Bernd (5 Replies)
Discussion started by: bjb
5 Replies

8. Shell Programming and Scripting

Backticks within backticks?

Hi, I'm trying to do something like this: range= `expr `date '+%m'` - 1` and it does not work. How can I tell it to evaluate an expression within another expression evaluation? I was at first worried that `date '+%m'` would return a string but apparently expr does the math okay normally, so the... (3 Replies)
Discussion started by: jeriryan87
3 Replies

9. Shell Programming and Scripting

Perl - backticks v system in if statements

Can someone explain the difference between backticks and system when evaluated in these if statements: sub getDate { print "start date\n"; if ( system("/bin/date") ) { print "can't get date\n"; exit(2); } print "finish date\n"; } Returns the following: start date Thu... (5 Replies)
Discussion started by: gjkeenan
5 Replies
Login or Register to Ask a Question