:(冒號)GNU Bash內置的目的是什麼?

本文翻譯自:What is the purpose of the : (colon) GNU Bash builtin?

What is the purpose of a command that does nothing, being little more than a comment leader, but is actually a shell builtin in and of itself? 什麼都不做,僅是註釋領導者,但實際上是內置於其自身的shell的目的是什麼?

It's slower than inserting a comment into your scripts by about 40% per call, which probably varies greatly depending on the size of the comment. 這比每次調用將註釋插入腳本要慢40%,這取決於註釋的大小,差別可能很大。 The only possible reasons I can see for it are these: 我能看到的唯一可能原因是:

# poor man's delay function
for ((x=0;x<100000;++x)) ; do : ; done

# inserting comments into string of commands
command ; command ; : we need a comment in here for some reason ; command

# an alias for `true' (lazy programming)
while : ; do command ; done

I guess what I'm really looking for is what historical application it might have had. 我想我真正要尋找的是它可能具有的歷史應用。


#1樓

參考:https://stackoom.com/question/DWwA/冒號-GNU-Bash內置的目的是什麼


#2樓

: can also be for block comment (similar to /* */ in C language). :也可以用於塊註釋(類似於C語言中的/ * * /)。 For example, if you want to skip a block of code in your script, you can do this: 例如,如果您想跳過腳本中的代碼塊,則可以執行以下操作:

: << 'SKIP'

your code block here

SKIP

#3樓

Two more uses not mentioned in other answers: 其他答案中未提及的另外兩種用途:

Logging 記錄中

Take this example script: 使用以下示例腳本:

set -x
: Logging message here
example_command

The first line, set -x , makes the shell print out the command before running it. 第一行set -x使外殼程序在運行之前打印出命令。 It's quite a useful construct. 這是一個非常有用的構造。 The downside is that the usual echo Log message type of statement now prints the message twice. 缺點是現在通常的echo Log message類型的語句將消息打印兩次。 The colon method gets round that. 冒號方法可以解決這個問題。 Note that you'll still have to escape special characters just like you would for echo . 請注意,您仍然必須像echo一樣轉義特殊字符。

Cron job titles Cron職位

I've seen it being used in cron jobs, like this: 我已經看到它在cron作業中使用,像這樣:

45 10 * * * : Backup for database ; /opt/backup.sh

This is a cron job that runs the script /opt/backup.sh every day at 10:45. 這是一項cron作業,每天10:45運行腳本/opt/backup.sh The advantage of this technique is that it makes for better looking email subjects when the /opt/backup.sh prints some output. 此技術的優點是,當/opt/backup.sh打印一些輸出時,它可使電子郵件主題看起來更好。


#4樓

Historically , Bourne shells didn't have true and false as built-in commands. 從歷史上看 ,Bourne shell沒有像內置命令那樣具有truefalse true was instead simply aliased to : , and false to something like let 0 . 相反,只將true別名爲: ,將false別名爲let 0

: is slightly better than true for portability to ancient Bourne-derived shells. :對於移植到古代Bourne衍生的炮彈而言,它比true要好得多。 As a simple example, consider having neither the ! 作爲一個簡單的示例,請考慮不使用! pipeline operator nor the || 管道運算符或|| list operator (as was the case for some ancient Bourne shells). 列表運算符(某些古老的Bourne貝殼就是這種情況)。 This leaves the else clause of the if statement as the only means for branching based on exit status: 這將if語句的else子句作爲基於退出狀態進行分支的唯一方法:

if command; then :; else ...; fi

Since if requires a non-empty then clause and comments don't count as non-empty, : serves as a no-op. 由於if需要一個非空的then子句和註釋,則不算作非空的,因此:用作no-op。

Nowadays (that is: in a modern context) you can usually use either : or true . 如今 (即:在現代環境中),您通常可以使用:true Both are specified by POSIX, and some find true easier to read. 兩者都是由POSIX指定的,有些true容易閱讀。 However there is one interesting difference: : is a so-called POSIX special built-in , whereas true is a regular built-in . 然而,有一個有趣的差異: : 內置了一個所謂的POSIX 特殊的 ,而true是一個普通的內置

  • Special built-ins are required to be built into the shell; 需要在外殼中內置特殊的內置插件。 Regular built-ins are only "typically" built in, but it isn't strictly guaranteed. 常規的內置程序只是“通常”內置的,但並不能嚴格保證。 There usually shouldn't be a regular program named : with the function of true in PATH of most systems. 通常有不應該命名爲正常計劃:與功能true在大多數系統的路徑。

  • Probably the most crucial difference is that with special built-ins, any variable set by the built-in - even in the environment during simple command evaluation - persists after the command completes, as demonstrated here using ksh93: 可能最關鍵的區別在於,使用特殊的內置程序,即使在簡單的命令評估過程中,內置程序設置的任何變量(即使在環境中)也將在命令完成後仍然存在,如以下使用ksh93所示:

     $ unset x; ( x=hi :; echo "$x" ) hi $ ( x=hi true; echo "$x" ) $ 

    Note that Zsh ignores this requirement, as does GNU Bash except when operating in POSIX compatibility mode, but all other major "POSIX sh derived" shells observe this including dash, ksh93, and mksh. 請注意,除了在POSIX兼容模式下運行時,Zsh都會忽略此要求,GNU Bash也會忽略此要求,但是所有其他主要的“ POSIX sh派生”外殼程序都遵守此要求,包括破折號,ksh93和mksh。

  • Another difference is that regular built-ins must be compatible with exec - demonstrated here using Bash: 另一個區別是常規的內置程序必須與exec兼容-在此處使用Bash進行了演示:

     $ ( exec : ) -bash: exec: :: not found $ ( exec true ) $ 
  • POSIX also explicitly notes that : may be faster than true , though this is of course an implementation-specific detail. POSIX還明確指出:可能比true快,儘管這當然是特定於實現的細節。


#5樓

You could use it in conjunction with backticks ( `` ) to execute a command without displaying its output, like this: 您可以將其與反引號( `` )結合使用以執行命令而不顯示其輸出,如下所示:

: `some_command`

Of course you could just do some_command > /dev/null , but the : -version is somewhat shorter. 當然,您可以執行some_command > /dev/null ,但是: -version會更短一些。

That being said I wouldn't recommend actually doing that as it would just confuse people. 話雖這麼說,但我不建議您實際這樣做,因爲這會讓人們感到困惑。 It just came to mind as a possible use-case. 只是想到了一個可能的用例。


#6樓

It's similar to pass in Python. 類似於在Python中pass

One use would be to stub out a function until it gets written: 一種用途是將一個函數存根,直到函數被寫入:

future_function () { :; }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章