Difference between revisions of "Bash operators"

From Linuxintro
m
 
(3 intermediate revisions by one other user not shown)
Line 6: Line 6:
 
These wildcards are actually bash operators! Imagine, we have the files a1, a2:
 
These wildcards are actually bash operators! Imagine, we have the files a1, a2:
 
  cp a*
 
  cp a*
 +
 
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
 +
rpm -ql $(rpm -qa)
 +
 
 +
for i in $(seq 1 1 100); do echo $i; done
  
 
= $(()) =
 
= $(()) =
Line 22: Line 29:
 
Example:
 
Example:
 
  # kill -9 $$
 
  # kill -9 $$
 +
 
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:
 +
  # [[xosview]] & [[sleep]] 5; [[kill]] $!
 +
 
 +
shows xosview for 5 seconds and stops it
  
 
= > and >> =
 
= > and >> =
Line 44: Line 58:
 
  cat notexisting 2>&1  # Outputs "File or directory not found." to stdout
 
  cat notexisting 2>&1  # Outputs "File or directory not found." to stdout
  
Please let me know if you're looking for a auhtor for your blog. You have some really good articles and I think I would be a good asset. If you ever want to take some of the load off, I'd love to write some content for your blog in exchange for a link back to mine. Please shoot me an email if interested. Kudos!|Have you ever considered about adding a little bit more than just your articles? I mean, what you say is valuable and everything. However think of if you added some great photos or video clips to give your posts more,  pop ! Your content is excellent but with images and videos, this website could certainly be one of the best in its field. Amazing blog!|Neat blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple adjustements would really make my blog shine. Please let me know where you got your design. Many thanks|Hi would you mind stating which blog platform you're working with? I'm looking to start my own blog in the near future but I'm having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I'm looking for something unique.                  P.S Sorry for getting off-topic but I had to ask!|Howdy just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Ie. I'm not sure if this is a formatting issue or something to do with browser compatibility but I figured I'd post to let you know. The layout look great though! Hope you get the issue fixed soon. Thanks|With havin so much written content do you ever run into any issues of plagorism or copyright violation? My website has a lot of unique content I've either created myself or outsourced but it looks like a lot of it is popping it up all over the web without my auhtorization. Do you know any ways to help prevent content from being stolen? I'd truly appreciate it.|Have you ever thought about publishing an e-book or guest auhtoring on other blogs? I have a blog centered on the same ideas you discuss and would really like to have you share some stories/information. I know my visitors would appreciate your work. If you are even remotely interested, feel free to shoot me an email.|Hi! Someone in my Facebook group shared this website with us so I came to take a look. I'm definitely enjoying the information. I'm bookmarking and will be tweeting this to my followers! Exceptional blog and outstanding design.|Awesome blog! Do you have any helpful hints for aspiring writers? I'm planning to start my own blog soon but I'm a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I'm completely overwhelmed .. Any ideas? Bless you!|My programmer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using Movable-type on a number of websites for about a year and am anxious about switching to another platform. I have heard fantastic things about blogengine.net. Is there a way I can transfer all my wordpress posts into it? Any kind of help would be greatly appreciated!|Does your site have a contact page? I'm having a tough time locating it but, I'd like to shoot you an email. I've got some recommendations for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it expand over time.|It's a shame you don't have a donate button! I'd certainly donate to this outstanding blog! I suppose for now i'll settle for bookmarking and adding your RSS feed to my Google account. I look forward to brand new updates and will talk about this website with my Facebook group. Chat soon!|Greetings from Los angeles! I'm bored to tears at work so I decided to check out your blog on my iphone during lunch break. I enjoy the information you provide here and can't wait to take a look when I get home. I'm amazed at how quick your blog loaded on my phone .. I'm not even using WIFI, just 3G .. Anyways, excellent site!|Hey there! I know this is kinda off topic but I'd figured I'd ask. Would you be interested in exchanging links or maybe guest auhtoring a blog post or vice-versa? My site discusses a lot of the same subjects as yours and I think we could greatly benefit from each other. If you're interested feel free to send me an email. I look forward to hearing from you! Fantastic blog by the way!
+
= | =
 +
| is used to pipe the stdout of one programm to the stdin of another one.
 +
tail -n 100 ''foo'' | sort -r  # Reverse sort the last 100 lines of the file ''foo''
  
 
= && =
 
= && =
 
&& 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  
 
  command1 && command2
 
  command1 && command2
 +
 
command2 will only be executed if command1 returned success.
 
command2 will only be executed if command1 returned success.
  

Latest revision as of 13:22, 21 March 2021

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

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

$(())

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

Examples:

# echo $((2*2))
4

$$

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

Example:

# kill -9 $$

kills the current process

$!

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

Example:

# xosview & sleep 5; kill $!

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:

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

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:

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

|

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

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

&&

&& 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

command1 && command2

command2 will only be executed if command1 returned success.

See also