博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux中backticks反引号的作用
阅读量:6452 次
发布时间:2019-06-23

本文共 1883 字,大约阅读时间需要 6 分钟。

This is a backtickA backtick is not a quotation sign. It has a very special meaning. Everything you type between backticks is evaluated (executed) by the shell before the main command (like chown in your examples), and the output of that execution is used by that command, just as if you'd type that output at that place in the command line.

So, what

sudo chown `id -u` /somedir

effectively runs (depending on your user ID) is:

sudo chown 1000 /somedir \ \ \ \ \ \ \ `-- the second argument to "chown" (target directory) \ \ `-- your user ID, which is the output of "id -u" command \ `-- "chown" command (change ownership of file/directory) `-- the "run as root" command; everything after this is run with root privileges

Have a look at  to learn why, in many situations, it is not a good idea to use backticks.

Btw, if you ever wanted to use a backtick literally, e.g. in a string, you can escape it by placing a backslash (\) before it.

  • 27
    This explains backticks pretty well, but using $(your expression)is a better way to do the same thing as $() allows you to nest expressions. for instance: cd $(dirname $(type -P touch)) will cd you into the directory containing the touch command –  
  • 9
    @KhajaMinhajuddin You're definitely right about nesting -  covers it in detail. But even though I think it is a good practise to use $() in most situations, it does not make backticks a worse thing. For practical purposes, one has to admit that they are much faster to type on the command line (2 keystrokes compared to at least 5, including Shift). –   
  • 2
    @rozcietrzewiacz Your latter remark is probably true for most keyboards but $( ) is definitely easier to type than ` ` at least on a French keyboard. –  
  • 1
    And another +1 for using backticks in the ASCII art ;) –   
  •  
    I wrote excape instead of escape and know I cannot fix that 1-letter mistake! ?‍♂️ – 

转载地址:http://njgwo.baihongyu.com/

你可能感兴趣的文章
Linux/Android 性能优化工具 perf
查看>>
learn go recursive
查看>>
对于double小数点后取两位
查看>>
HashMap的小试牛刀
查看>>
GitHub使用教程、注册与安装
查看>>
论以结果为导向
查看>>
蓝桥杯模拟五 蒜头君下棋
查看>>
CODE[VS] 1294 全排列
查看>>
<<The C Programming Language>>讀書筆記
查看>>
Lodop属性和方法详解
查看>>
(转)CSS的display属性
查看>>
如何在目录中查找具有指定字符串的文件(shell)
查看>>
安卓学习笔记2
查看>>
angularJs按需加载代码(未验证)
查看>>
选择排序
查看>>
DotNet(C#)自定义运行时窗体设计器 一
查看>>
P2627 修剪草坪[dp][单调队列]
查看>>
JS详细入门教程(上)
查看>>
Android学习笔记21-ImageView获取网络图片
查看>>
线段树分治
查看>>