There are certain commands or tricks that you start using as a sysadmin, which you simply incorporate into your arsenal and never really stop to analyze in-depth all the options or alternatives to them.
For me, one of those tricks is the backtick operator. I used it rather frequently when I programmed in Perl (which I don't use nowadays, but it seems to still have a loyal fan club - check this poll). In my Perl scripts, I would use the backtick operator to run a command in the operating system and return the output to continue the logic in the script.
[ You might also like: Bash scripting: How to write data to text files ]
The backtick operator is also available in shell scripts, and because it is so easy to combine with other commands, I started using it a lot. However, there is a more "recommended" way to do the same thing, using the $ parens (parentheses) operator.
In this article, I show you the pros and cons of each one for use in shell scripts.
The basic functionality of backticks
The Open Group has a definition for the backtick operator, officially referred to as command substitution. This operator is not the single quote character, but another character known as the grave accent (`).
The idea is simple:
❯ echo "There are `ls | wc -l` files in this directory"
There are 3 files in this directory
I have a very simple group of commands, ls | wc -l that I use to list and count the number of files in the current directory. I can easily do this interactively and see exactly what the result is. Then I can embed it in my main echo command.
In a shell script, I could certainly perform the same thing, assigning the result of my count command to a variable and then use the variable later.
❯ file_count=`ls | wc -l`
❯ echo "There are $file_count files in this directory"
There are 3 files in this directory
And of course, that would be better if the idea was to reuse the value or if the operation to perform was not so simple as in my example.
Embedding the commands using backticks is what I would classify as "quick and dirty tricks."
The $ parens
You can achieve the same result by replacing the backticks with the $ parens, like in the example below:
❯ echo "There are $(ls | wc -l) files in this directory"
There are 3 files in this directory
Here's another example, still very simple but a little more realistic. I need to troubleshoot something in my network connections, so I decide to show my total and waiting connections minute by minute.
❯ cat netinfo.sh
#!/bin/bash
while true
do
ss -an > netinfo.txt
connections_total=$(cat netinfo.txt | wc -l)
connections_waiting=$(grep WAIT netinfo.txt | wc -l)
printf "$(date +%R) - Total=%6d Waiting=%6d\n" $connections_total $connections_waiting
sleep 60
done
❯ ./netinfo.sh
22:59 - Total= 2930 Waiting= 977
23:00 - Total= 2923 Waiting= 963
23:01 - Total= 2346 Waiting= 397
23:02 - Total= 2497 Waiting= 541
It doesn't seem like a huge difference, right? I just had to adjust the syntax. Well, there are some implications involving the two approaches. If you are like me, who automatically uses the backticks without even blinking, keep reading.
Deprecation and recommendations
Deprecation sounds like a bad word, and in many cases, it might really be bad.
When I was researching the explanations for the backtick operator, I found some discussions about "are the backtick operators deprecated?"
The short answer is: Not in the sense of "on the verge of becoming unsupported and stop working." However, backticks should be avoided and replaced by the $ parens syntax.
The main reasons for that are (in no particular order):
1. Backticks operators can become messy if the internal commands also use backticks.
- You will need to escape the internal backticks, and if you have single quotes as part of the commands or part of the results, reading and troubleshooting the script can become difficult.
- If you start thinking about nesting backtick operators inside other backtick operators, things will not work as expected or not work at all. Don't bother.
2. The $ parens operator is safer and more predictable.
- What you code inside the
$parens operator is treated as a shell script. Syntactically it is the same thing as having that code in a text file, so you can expect that everything you would code in an isolated shell script would work here.
Here are some examples of the behavioral differences between backticks and $ parens:
❯ echo '\$x'
\$x
❯ echo `echo '\$x'`
$x
❯ echo $(echo '\$x')
\$x
You can find additional examples of the differences between backticks and $ parens behavior here.
[ Free cheat sheet: Get a list of Linux utilities and commands for managing servers and networks. ]
Wrapping up
If you compare the two approaches, it seems logical to think that you should always/only use the $ parens approach. And you might think that the backtick operators are only used by sysadmins from an older era.
Well, that might be true, as sometimes I use things that I learned long ago, and in simple situations, my "muscle memory" just codes it for me. For those ad-hoc commands that you know that do not contain any nasty characters, you might be OK using backticks. But for anything that is more perennial or more complex/sophisticated, please go with the $ parens approach.
저자 소개
Roberto Nozaki (RHCSA/RHCE/RHCA) is an Automation Principal Consultant at Red Hat Canada where he specializes in IT automation with Ansible. He has experience in the financial, retail, and telecommunications sectors, having performed different roles in his career, from programming in mainframe environments to delivering IBM/Tivoli and Netcool products as a pre-sales and post-sales consultant.
Roberto has been a computer and software programming enthusiast for over 35 years. He is currently interested in hacking what he considers to be the ultimate hardware and software: our bodies and our minds.
Roberto lives in Toronto, and when he is not studying and working with Linux and Ansible, he likes to meditate, play the electric guitar, and research neuroscience, altered states of consciousness, biohacking, and spirituality.
유사한 검색 결과
Behind the scenes of RHEL 10, part 3
Alliander modernises its electricity grid with Red Hat for long-term reliability in balance with rapid innovation
Linux, Shadowman, And Open Source Spirit | Compiler
The Overlooked Operating System | Compiler: Stack/Unstuck
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래