Difference between revisions of "Bash operators"

From Linuxintro
imported>ThorstenStaerk
m (Reverted edits by 221.178.182.30 (talk) to last revision by 173.183.218.83)
imported>ThorstenStaerk
Line 8: Line 8:
 
now is a valid bash command which overrides one of these files with the other one.
 
now is a valid bash command which overrides one of these files with the other one.
  
已經改好了 請 smart update 後安裝 oxim其實不安裝 qt-immodule 或 gtk-immodule 也可以但是預設的設定檔環境變數有設GTK_IM_MODULE=oxim 和 QT_IM_MODULE=oxim若真不需要將 /etc/X11/xim.d/oxim 中將上面兩個變數改成*_IM_MODULE=xim
+
= $() =
 +
The operator $() in the bash shell is replaced by the output of the command enclosed in the parentheses. It is equivalent to backticks (``), but can be cascaded more easily.
 +
 
 +
Examples
 +
<source>
 +
rpm -ql $(rpm -qa)
 +
 
 +
for i in $(seq 1 1 100); do echo $i; done
 +
</source>
  
 
= $(()) =
 
= $(()) =
Line 14: Line 22:
  
 
Examples:
 
Examples:
 +
<source>
 
  # echo $((2*2))
 
  # echo $((2*2))
 
  4
 
  4
 +
</source>
  
 
= $$ =
 
= $$ =
Line 21: Line 31:
  
 
Example:
 
Example:
 +
<source>
 
  # kill -9 $$
 
  # kill -9 $$
 +
</source>
 
kills the current process
 
kills the current process
  
Showtime gotta see us guys, im sorry i dont keep if they are real(The Real Lword) or not but these guys rocks the stage in every episode!!!! And this pecafrmonre,everytime i watch i got chills!!! Jennifer Beals such a great actress,such a great person,i adore her.And i cant believe that her best works are just Flashdance and The L World. She is 47 yrs old now.dont miss this woman
+
= $! =
 +
The operator $! delivers the id of the process that has most recently sent to the [[background]].
 +
 
 +
Example:
 +
<source>
 +
  # [[xosview]] & [[sleep]] 5; [[kill]] $!
 +
</source>
 +
shows xosview for 5 seconds and stops it
  
 
= > and >> =
 
= > and >> =
Line 31: Line 50:
  
 
Examples:
 
Examples:
 +
<source>
 
  ls -alh > files-in-directory.txt
 
  ls -alh > files-in-directory.txt
 
  ls -alh /otherdir >> files-in-directory.txt  # Append list of files in other directory
 
  ls -alh /otherdir >> files-in-directory.txt  # Append list of files in other directory
 +
</source>
  
 
= 2> or 2>> =
 
= 2> or 2>> =
Line 41: Line 62:
  
 
Examples:
 
Examples:
 +
<source>
 
  cat notexisting 2> test  # Writes "File or directory not found." to test
 
  cat notexisting 2> test  # Writes "File or directory not found." to test
 
  cat notexisting 2>&1  # Outputs "File or directory not found." to stdout
 
  cat notexisting 2>&1  # Outputs "File or directory not found." to stdout
 +
</source>
  
 
= | =
 
= | =
 
| is used to pipe the stdout of one programm to the stdin of another one.
 
| is used to pipe the stdout of one programm to the stdin of another one.
 +
<source>
 
  tail -n 100 ''foo'' | sort -r  # Reverse sort the last 100 lines of the file ''foo''
 
  tail -n 100 ''foo'' | sort -r  # Reverse sort the last 100 lines of the file ''foo''
 +
</source>
  
 
= && =
 
= && =
 
&& evaluates the binary result of the command left of it and ands it with the binary result right of it if it is not already false, where false!=0. In other words, if you write  
 
&& evaluates the binary result of the command left of it and ands it with the binary result right of it if it is not already false, where false!=0. In other words, if you write  
 +
<source>
 
  command1 && command2
 
  command1 && command2
 +
</source>
 
command2 will only be executed if command1 returned success.
 
command2 will only be executed if command1 returned success.
  

Revision as of 05:42, 3 April 2020

You have a shell script and wonder what these operators do? Then this article is for you.

* and ?

You may already know these used for so called "shell globbing" (pattern matching). * replaces an arbitrary amount of characters (including none), while ? replaces exactly one character.

These wildcards are actually bash operators! Imagine, we have the files a1, a2:

cp a*

now is a valid bash command which overrides one of these files with the other one.

$()

The operator $() in the bash shell is replaced by the output of the command enclosed in the parentheses. It is equivalent to backticks (``), but can be cascaded more easily.

Examples <source>

rpm -ql $(rpm -qa)
for i in $(seq 1 1 100); do echo $i; done

</source>

$(())

The operator $(()) in the bash shell is replaced by the arithmetic result of the expression enclosed in the parentheses.

Examples: <source>

# echo $((2*2))
4

</source>

$$

The operator $$ delivers the id of the currently-running process.

Example: <source>

# kill -9 $$

</source> kills the current process

$!

The operator $! delivers the id of the process that has most recently sent to the background.

Example: <source>

# xosview & sleep 5; kill $!

</source> shows xosview for 5 seconds and stops it

> and >>

> and >> can be used to redirect the output (only stdout, not stderr, for that see below) of a command to a file. The difference between them is, that >> appends to the given files, while > will truncate it.

Examples: <source>

ls -alh > files-in-directory.txt
ls -alh /otherdir >> files-in-directory.txt  # Append list of files in other directory

</source>

2> or 2>>

stderr can be redirected by using 2> or 2>>.

But sometimes, you need stderr to be included in stdout for certain purpose, e.g. when another program is parsing/logging only stdout and you need errors to be in there, too. In these cases, you can redirect stderr to stdout with "2>&1". (Of course, it is also possible to redirect stdout to stderr this way, if you need to silence a program on stdout for some purpose).

Examples: <source>

cat notexisting 2> test  # Writes "File or directory not found." to test
cat notexisting 2>&1  # Outputs "File or directory not found." to stdout

</source>

|

| is used to pipe the stdout of one programm to the stdin of another one. <source>

tail -n 100 foo | sort -r  # Reverse sort the last 100 lines of the file foo

</source>

&&

&& evaluates the binary result of the command left of it and ands it with the binary result right of it if it is not already false, where false!=0. In other words, if you write <source>

command1 && command2

</source> command2 will only be executed if command1 returned success.

See also