Skip to main content
added 233 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

The input to xargs -L1 or xargs -l is almost a list of lines, but not quite — if there is a space atto split the endinput on lines, but to run one command per line of ainput (that line still split to make up the arguments, and continued on the followingnext line is a continuationif ending in blanks).

xargs -I PLACEHOLDER does use one line of input to substitute the PLACEHOLDER but quotes and backslashes are still processed and leading blanks trimmed.

You can use xargs -0r0 where applicable (and where available: GNU (Linux, Cygwin), BusyBox, BSDBSDs, OSX, but it isn't in POSIX). That's safe, because null bytes can't appear in most data, in particular in file names and external command arguments. To produce a null-separated list of file names, use find … -print0 (or you can use find … -exec … as explained below).

The input to xargs -L1 or xargs -l is almost a list of lines, but not quite — if there is a space at the end of a line, the following line is a continuation line.

You can use xargs -0 where applicable (and where available: GNU (Linux, Cygwin), BusyBox, BSD, OSX, but it isn't in POSIX). That's safe, because null bytes can't appear in most data, in particular in file names. To produce a null-separated list of file names, use find … -print0 (or you can use find … -exec … as explained below).

xargs -L1 or xargs -l is not to split the input on lines, but to run one command per line of input (that line still split to make up the arguments, and continued on the next line if ending in blanks).

xargs -I PLACEHOLDER does use one line of input to substitute the PLACEHOLDER but quotes and backslashes are still processed and leading blanks trimmed.

You can use xargs -r0 where applicable (and where available: GNU (Linux, Cygwin), BusyBox, BSDs, OSX, but it isn't in POSIX). That's safe, because null bytes can't appear in most data, in particular in file names and external command arguments. To produce a null-separated list of file names, use find … -print0 (or you can use find … -exec … as explained below).

Note about commands that take options starting with +
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

On a related note, keep in mind that file names can begin with a - (dash/minus), which most commands interpret as denoting an option. Some commands (like sh, set or sort) also accept options that start with +. If you have a file name that begins with a variable part, be sure to pass -- before it, as in the snippet above. This indicates to the command that it has reached the end of options, so anything after that is a file name even if it starts with - or +.

Alternatively, you can make sure that your file names begin with a character other than -. Absolute file names begin with /, and you can add ./ at the beginning of relative names. The following snippet turns the content of the variable f into a “safe” way of referingreferring to the same file that's guaranteed not to start with - nor +.

case "$f" in -* | +*) "f=./$f";; esac 

On a related note, keep in mind that file names can begin with a - (dash/minus), which most commands interpret as denoting an option. If you have a file name that begins with a variable part, be sure to pass -- before it, as in the snippet above. This indicates to the command that it has reached the end of options, so anything after that is a file name even if it starts with -.

Alternatively, you can make sure that your file names begin with a character other than -. Absolute file names begin with /, and you can add ./ at the beginning of relative names. The following snippet turns the content of the variable f into a “safe” way of refering to the same file that's guaranteed not to start with -.

case "$f" in -*) "f=./$f";; esac 

On a related note, keep in mind that file names can begin with a - (dash/minus), which most commands interpret as denoting an option. Some commands (like sh, set or sort) also accept options that start with +. If you have a file name that begins with a variable part, be sure to pass -- before it, as in the snippet above. This indicates to the command that it has reached the end of options, so anything after that is a file name even if it starts with - or +.

Alternatively, you can make sure that your file names begin with a character other than -. Absolute file names begin with /, and you can add ./ at the beginning of relative names. The following snippet turns the content of the variable f into a “safe” way of referring to the same file that's guaranteed not to start with - nor +.

case "$f" in -* | +*) "f=./$f";; esac 
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Ksh88 has array variables with a different assignment syntax set -A myfiles "someprefix"*.txt (see assignation variable under different ksh environmentassignation variable under different ksh environment if you need ksh88/bash portability). Bourne/POSIX-style shells have a single one array, the array of positional parameters "$@" which you set with set and which is local to a function:

Ksh88 has array variables with a different assignment syntax set -A myfiles "someprefix"*.txt (see assignation variable under different ksh environment if you need ksh88/bash portability). Bourne/POSIX-style shells have a single one array, the array of positional parameters "$@" which you set with set and which is local to a function:

Ksh88 has array variables with a different assignment syntax set -A myfiles "someprefix"*.txt (see assignation variable under different ksh environment if you need ksh88/bash portability). Bourne/POSIX-style shells have a single one array, the array of positional parameters "$@" which you set with set and which is local to a function:

replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link
Loading
missing closing double quote
Source Link
fedorqui
  • 8.2k
  • 9
  • 38
  • 76
Loading
yash has arrays (except when called as sh)
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading
added 251 characters in body
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading
you need two backslashes for double-quotes AND two backslashes for sed RHS. dont suspend me mods.
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 242
Loading
added a section on storing a command in a variable
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading
added 136 characters in body
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading
mention the dangers of file names beginning with -
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading
added 229 characters in body
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading
improve explanation of "$@"; simplify the discussion of arrays; mention arithmetic expressions
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading
`for x; do` is neither Bourne nor POSIX. `for x do` is. Use something descriptive for $0 as that's used in error messages.
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading
added 285 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading
added a paragraph about xargs -0
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading