I think that xargs is intended exactly for this, but I tried using it and couldn't figure it out.   I have a command foo -bar 123 --baz=whatever. I want a Bash one-liner to run three copies of it in parallel, except they will have a different argument for 123. Say the first copy will be 123, the second one is 234 and the third one is 345. I want the three commands to execute in parallel.  How can I do this?
61906
I have a bunch of PNG images on a directory. I have an application called pngout that I run to compress these images. This application is called by a script I did. The problem is that this script does one at a time, something like this:  FILES=(./*.png) for f in  "${FILES[@]}" do         echo "Processing $f file..."         # take action on each file. $f store current file name         ./pngout -s0 $f R${f/\.\//} done   Processing just one file at a time, takes a lot of time. After running this app, I see that the CPU is just 10%. So I discovered that I can divide these files in 4 batches, put each batch in a directory and fire 4, from four terminal windows, four processes, so I have four instances of my script, at the same time, processing those images and the job takes 1/4 of the time.  The second problem is that I lost time dividing the images and batches and copying the script to four directories, open 4 terminal windows, bla bla...  How do that with one script, without having to divide anything?  I mean two things: first how do I from a bash script, fire a process to the background ? (just add &amp; to the end?) Second: how do I stop sending tasks to the background after sending the fourth tasks and put the script to wait until the tasks end? I mean, just sending a new task to the background as one tasks end, keeping always 4 tasks in parallel? if I do not do that the loop will fire zillions of tasks to the background and the CPU will clog.
86279
Here is the behaviour I want to understand:  $ ps   PID TTY           TIME CMD   392 ttys000    0:00.20 -bash  4268 ttys000    0:00.00 xargs $ kill 4268 $ ps   PID TTY           TIME CMD   392 ttys000    0:00.20 -bash [1]+  Terminated: 15          xargs $ ps   PID TTY           TIME CMD   392 ttys000    0:00.21 -bash   Why does it show the [1]+  Terminated: 15          xargs after I kill a process, instead of just not showing it as it was just killed?  I'm using bash on Mac OS X 10.7.5.
42231
One of my coworkers has provided me with a Bash syntax that I am unfamiliar with.  My Google foo has failed me on figuring out what it does and why/when I should use it.    The command that he sent me was of this form:  someVariable=something command   Initially, I thought that this was equivalent to the following:   someVariable=something ; command   Or  someVariable=something  command   But this doesn't appear to the be case.  Examples:   [Jan-03 11:26][~]$ # Look at the environment variable BAZ. It is currently empty [Jan-03 11:26][~]$ echo $BAZ  [Jan-03 11:27][~]$ # Try running a command of the same format     [Jan-03 11:27][~]$ BAZ=jake echo $BAZ  [Jan-03 11:27][~]$     [Jan-03 11:27][~]$ # Now, echo BAZ again. It is still empty:     [Jan-03 11:27][~]$ echo $BAZ  [Jan-03 11:27][~]$     [Jan-03 11:28][~]$     [Jan-03 11:28][~]$ # If we add a semi-colon to the command, we get dramatically different results: [Jan-03 11:28][~]$ BAZ=jake ; echo $BAZ jake [Jan-03 11:28][~]$ [Jan-03 11:28][~]$ # And we can see that the variable is actually set: [Jan-03 11:29][~]$ echo $BAZ jake [Jan-03 11:29][~]$   What does this syntax do?  What happens to the variable that has been set?  Why does this work?
47502
I remembered bash can do the following:  Suppose there are 3 files: a,b,c in the current directory, when I input:  $&gt; somecommand *   and then press a key or key combinations or a key sequence, the wildcard * online expanded as:  $&gt; somecommand a b c   but I cannot remember and cannot google out the key sequence! help!
19837
I want to know if I can pass an argument with an alias command.  for example:  alias d="dmesg|grep -iw usb|tail -5"    Now d will print the last 5 lines. If I want to use d to print a different number of lines, I have to make change in the alias command declaration of d again.  Is there any way I can modify the declaration of an alias so that I don't have to retype the declaration to change the number of lines. Like incorporating passing the number of lines as an argument while declaring alias for d? Or is there some other method to solve this?
56238
Bash 4.2 on CentOS 6.5:  In my ~/.bash_profile I have a bunch of aliases, including:  alias grep='grep -n --color=always'   so that I can get color highlighting and print line numbers automatically when running grep. If I run the following, highlighting works as expected:  $ grep -Re 'regex_here' *.py   However, when I ran this recently:  $ find . -name '*.py' | xargs grep -E 'regex_here'   the results were not highlighted and line numbers weren't printed, forcing me to go back and explicitly add -n --color=always to the grep command.   Does xargs not read aliases in the environment? If not, is there a way to make it do that?
115643
If I just use basename {} .txt, it will work:  find . -iname "*.txt" -exec basename {} .txt \;   It will just print xxx instead of ./xxx.txt  if I want use $(basename {} .txt) in the -exec option, it will fail:  find . -iname "*.txt" -exec echo "$(basename {} .txt)" \;   It will just print ./xxx.txt  How can I solve this problem? I hope I can use $(basename {} .txt) as parameters for other cmd. Do I have to do sh -c or pipe -exec basename {} \; with xargs?
21443
Is there any standard that covers the portability of running a command after variable assignment on the same line?  APPLE="cider" echo hi   How portable is something like that? Where will it work and where won't it?   Also: my shell scripts start with #!/bin/sh if that makes any difference.
24737
Lets say I have a loop in Bash:  for foo in `some-command` do    do-something $foo done   do-something is cpu bound and I have a nice shiny 4 core processor. I'd like to be able to run up to 4 do-something's at once.  The naive approach seems to be:  for foo in `some-command` do    do-something $foo &amp; done   This will run all do-somethings at once, but there are a couple downsides, mainly that do-something may also have some significant I/O which performing all at once might slow down a bit. The other problem is that this code block returns immediately, so no way to do other work when all the do-somethings are finished.  How would you write this loop so there are always X do-somethings running at once?
89074
It seems that a common operation needed when defining new commands is to scan over a list of arguments (separated e.g. by a comma, but maybe by something else) and do something with each argument in the list, find the last one, or whatever.  For example to write commands that look like \Command{a,b,c} or \Command{c.d.e} and do something with each a, b, and c. See e.g. questions on  and .  Some solutions have been posted there, but what would be the best idiom to perform such kind of operations? LaTeX's \@for looks very nice, but is it possible to use it to scan anything other than comma-separated lists?
69323
I have the following commands that I want to have them all in one shell file:  su – s3 lizinfo.sh –b   when the first linex executes, it prompts and the command prompt become like as below:   -bash-3.1$ (here I cant type second command &gt;&gt; lizinfo.sh –b)   I want to have my shell file run all of these command at once. But when I run my shell file it only runs the first line and for does not run the second line, and waits until I enter it manually.  Would you help me please ?
72278
How do I execute a program X times in the terminal?  I read that I have to do a bin/bash txt, but I don't know how to execute this X times.
45642
Say I have written two commands, CommandA and CommandB. I wish to have a third command that takes inputs to determines which of the two previous commands to execute. Very naively this how I would try and accomplish this as shown below, however, it does not work.  \newcommand{CommandA}{     \hspace{5mm} } \newcommand{CommandB}{     \vspace{5mm} } \newcommand{C}[1]{     \Command#1 }   So if I call \C{A}, as written I would really execute \CommandA.
79894
Can you explain what this command does:  files=(this_is_filename) for filename in ${files[@]}; do    ls -t1 ../htory/$filename* |     head -1 |     xargs -I fname cp -p fname ../htory2/somefile.CSV done   Especially this part of this command  xargs -I fname cp -p fname ../htory2/somefile.CSV
16378
Right now I have one-liner like this:  curl -fsSL http://git.io/vvZMn | bash   It is downloading script and passing it to bash as stdin file. I would like to run this script with additional argument print.  Maybe something like this?  curl -fsSL http://git.io/vvZMn | bash -- print   But this doesn't work.
651
I have a complex command that I'd like to make a shell/bash script of.  I can write it in terms of $1 easily:  foo $1 args -o $1.ext   I want to be able to pass multiple input names to the script. What's the right way to do it?    And, of course, I want to handle filenames with spaces in them.
112099
Bash manual says:     Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment,   except that traps caught by the shell are reset to the values that the shell inherited from its parent at invocation.   In this example,b isn't an environment variable, so b doesn't exist in the subshell created by command substitution. Then why is c assigned the value of b by command substituion? Is it because the parameter expansion happens for $b in the shell process before creating a subshell to execute echo 1?  $ b=1 $ c=$(echo $b) $ echo $c 1