Skip to main content
simplify a bit
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

Here,

bash -c date +%z

The first non-option argument after the -c is date, that's the command line / shell code / inline script that Bash will parse and run. The arguments after that are assigned to the shell or script name $0 (which makes up the inline script name, herehere, +%z), then the positional parameters $1, $2 ... (all unset here).

The string +%z is not discarded, but since the command date does not make use of $0 (or $1 etc.), and because bash didn't report any error, it does not affect what the shell does. It would get used if the shell had to print an error message, however. E.g.:

$ env PATH=/nowhere /bin/bash -c datedatexxx +%z
+%z: datedatexxx: command not found

The script couldn't find date inthe command $PATHdatexxx, so it did output an error message, prefixing it with the name that we gave to the script (+%z).

 

You'llTo use the arguments that come after -c and the shell code, you'll need to pass a command that uses the script name in $0 or the positional parameters $1..., e.g.:

% bash -c 'echo "script name: $0", first arg: "$1"' foo bar doo
script name: foo, first arg: bar

Though if you want to pass the argument list to another command as-is, you'd rather use "$@" (with the quotes) which expands to all positional parameters as distinct fields (as if you'd used "$1" "$2" ... for all the set positional parameters).

So (assuming GNU date for -d):

% bash -c 'date "$@"' my-inline-script +"%F %T" -d '1 Jan 2001'
2001-01-01 00:00:00

(HereAgain, that my-inline-script goes to $0 and gets only used if the shell needs to print an error message. The example above should show why it makes sense to put someething descriptive there. In any case, we need to pass some value there as $0 is filled before the positional parameters.)

Here,

bash -c date +%z

The first non-option argument after the -c is date, that's the command line / shell code / inline script that Bash will parse and run. The arguments after that are assigned to $0 (which makes up the inline script name, here, +%z), then the positional parameters $1, $2 ... (all unset here).

The string +%z is not discarded, but since the command date does not make use of $0 (or $1 etc.), and because bash didn't report any error, it does not affect what the shell does.

$ env PATH=/nowhere /bin/bash -c date +%z
+%z: date: command not found

The script couldn't find date in $PATH, so it did output an error message, prefixing it with the name that we gave to the script (+%z).

You'll need to pass a command that uses the script name in $0 or the positional parameters $1..., e.g.:

% bash -c 'echo "script name: $0", first arg: "$1"' foo bar doo
script name: foo, first arg: bar

Though if you want to pass the argument list to another command as-is, you'd rather use "$@" (with the quotes) which expands to all positional parameters as distinct fields (as if you'd used "$1" "$2" ... for all the set positional parameters).

So (assuming GNU date for -d):

% bash -c 'date "$@"' my-inline-script +"%F %T" -d '1 Jan 2001'
2001-01-01 00:00:00

(Here, that my-inline-script goes to $0 and gets only used if the shell needs to print an error message. In any case, we need to pass some value there as $0 is filled before the positional parameters.)

Here,

bash -c date +%z

The first non-option argument after the -c is date, that's the shell code that Bash will parse and run. The arguments after that are assigned to the shell or script name $0 (here, +%z), then the positional parameters $1, $2 ... (all unset here).

The string +%z is not discarded, but since the command date does not make use of $0 (or $1 etc.), it does not affect what the shell does. It would get used if the shell had to print an error message, however. E.g.:

$ /bin/bash -c datexxx +%z
+%z: datexxx: command not found

The script couldn't find the command datexxx, so it did output an error message, prefixing it with the name that we gave to the script (+%z).

 

To use the arguments that come after -c and the shell code, you'll need to pass a command that uses the script name in $0 or the positional parameters $1..., e.g.:

% bash -c 'echo "script name: $0", first arg: "$1"' foo bar doo
script name: foo, first arg: bar

Though if you want to pass the argument list to another command as-is, you'd rather use "$@" (with the quotes) which expands to all positional parameters as distinct fields (as if you'd used "$1" "$2" ... for all the set positional parameters).

So (assuming GNU date for -d):

% bash -c 'date "$@"' my-inline-script +"%F %T" -d '1 Jan 2001'
2001-01-01 00:00:00

(Again, that my-inline-script goes to $0 and gets only used if the shell needs to print an error message. The example above should show why it makes sense to put someething descriptive there. In any case, we need to pass some value there as $0 is filled before the positional parameters.)

added 343 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

Here,

bash -c date +%z

The first non-option argument after the -c is date, that's the command line / shell code / inline script that Bash will parse and run. The arguments after that are assigned to $0 (herewhich makes up the inline script name, here, +%z), then the positional parameters $1, $2 ... (all unset here).

The string +%z is not discarded, but since the command date does not make use of $0 (or $1 etc.), and because bash didn't report any error, it does not affect what the shell does.

$ env PATH=/nowhere /bin/bash -c date +%z
+%z: date: command not found

The script couldn't find date in $PATH, so it did output an error message, prefixing it with the name that we gave to the script (+%z).

You'll need to pass a command that uses the shellscript name in $0 or the positional parameters $1..., e.g.:

% bash -c 'echo "shell"script name: $0", first arg: "$1"' foo bar doo
shellscript name: foo, first arg: bar

Though if you want to pass the argument list to another command as-is, you'd rather use "$@" (with the quotes) which expands to all positional parameters as distinct fields (as if you'd used "$1" "$2" ... for all the set positional parameters).

So (assuming GNU date for -d):

% bash -c 'date "$@"' shmy-inline-script +"%F %T" -d '1 Jan 2001'
2001-01-01 00:00:00

(Here, that shmy-inline-script goes to $0 and gets only used if the shell needs to print an error message. In any case, we need to pass some value there as $0 is filled before the positional parameters.)

Here,

bash -c date +%z

The first non-option argument after the -c is date, that's the command line Bash will parse and run. The arguments after that are assigned to $0 (here, +%z), then the positional parameters $1, $2 ... (all unset here).

The string +%z is not discarded, but since the command date does not make use of $0 (or $1 etc.) it does not affect what the shell does.

You'll need to pass a command that uses the shell name in $0 or the positional parameters $1..., e.g.:

% bash -c 'echo "shell name: $0", first arg: "$1"' foo bar doo
shell name: foo, first arg: bar

Though if you want to pass the argument list to another command as-is, you'd rather use "$@" (with the quotes) which expands to all positional parameters as distinct fields (as if you'd used "$1" "$2" ... for all the set positional parameters).

So (assuming GNU date for -d):

% bash -c 'date "$@"' sh +"%F %T" -d '1 Jan 2001'
2001-01-01 00:00:00

(Here, that sh goes to $0 and gets only used if the shell needs to print an error message. In any case, we need to pass some value there as $0 is filled before the positional parameters.)

Here,

bash -c date +%z

The first non-option argument after the -c is date, that's the command line / shell code / inline script that Bash will parse and run. The arguments after that are assigned to $0 (which makes up the inline script name, here, +%z), then the positional parameters $1, $2 ... (all unset here).

The string +%z is not discarded, but since the command date does not make use of $0 (or $1 etc.), and because bash didn't report any error, it does not affect what the shell does.

$ env PATH=/nowhere /bin/bash -c date +%z
+%z: date: command not found

The script couldn't find date in $PATH, so it did output an error message, prefixing it with the name that we gave to the script (+%z).

You'll need to pass a command that uses the script name in $0 or the positional parameters $1..., e.g.:

% bash -c 'echo "script name: $0", first arg: "$1"' foo bar doo
script name: foo, first arg: bar

Though if you want to pass the argument list to another command as-is, you'd rather use "$@" (with the quotes) which expands to all positional parameters as distinct fields (as if you'd used "$1" "$2" ... for all the set positional parameters).

So (assuming GNU date for -d):

% bash -c 'date "$@"' my-inline-script +"%F %T" -d '1 Jan 2001'
2001-01-01 00:00:00

(Here, that my-inline-script goes to $0 and gets only used if the shell needs to print an error message. In any case, we need to pass some value there as $0 is filled before the positional parameters.)

added 256 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

Here,

bash -c date +%z

The first non-option argument after the -c is date, that's the command line Bash will parse and run. The arguments after that are assigned to $0 (here, +%z), then the positional parameters $1, $2 ... (unsetall unset here).

HoweverThe string +%z is not discarded, but since the command you gavedate does not make use of $0, or (or $1 etc. so that +%z) it does not affect what the resultshell does.

You'll need to pass a command that uses the shell name in $0 or the positional parameters $1..., e.g.:

% bash -c 'echo "shell name: $0", first arg: "$1"' foo bar doo
shell name: foo, first arg: bar

Though if you want to pass the argument list to another command as-is, you'd rather use "$@" (with the quotes) which expands to all positional parameters as distinct fields (as if you'd used "$1" "$2" ... for all the set positional parameters).

So (assuming GNU date for -d):

% bash -c 'date "$@"' sh +"%F %T" -d '1 Jan 2001'
2001-01-01 00:00:00

(Here, that sh goes to $0 and gets only used if the shell needs to print an error message. In any case, we need to pass some value there as $0 is filled before the positional parameters.)

Here,

bash -c date +%z

The first non-option argument after the -c is date, that's the command line Bash will parse and run. The arguments after that are assigned to $0 (here, +%z), then $1, $2 ... (unset here).

However, the command you gave does not make use of $0, or $1 etc. so that +%z does not affect the result.

You'll need to pass a command that uses the shell name in $0 or the positional parameters $1..., e.g.:

% bash -c 'echo "shell name: $0", first arg: "$1"' foo bar doo
shell name: foo, first arg: bar

Though if you want to pass the argument list to another command as-is, you'd rather use "$@" (with the quotes) which expands to all positional parameters as distinct fields (as if you'd used "$1" "$2" ... for all the set positional parameters).

So (assuming GNU date for -d):

% bash -c 'date "$@"' sh +"%F %T" -d '1 Jan 2001'
2001-01-01 00:00:00

Here,

bash -c date +%z

The first non-option argument after the -c is date, that's the command line Bash will parse and run. The arguments after that are assigned to $0 (here, +%z), then the positional parameters $1, $2 ... (all unset here).

The string +%z is not discarded, but since the command date does not make use of $0 (or $1 etc.) it does not affect what the shell does.

You'll need to pass a command that uses the shell name in $0 or the positional parameters $1..., e.g.:

% bash -c 'echo "shell name: $0", first arg: "$1"' foo bar doo
shell name: foo, first arg: bar

Though if you want to pass the argument list to another command as-is, you'd rather use "$@" (with the quotes) which expands to all positional parameters as distinct fields (as if you'd used "$1" "$2" ... for all the set positional parameters).

So (assuming GNU date for -d):

% bash -c 'date "$@"' sh +"%F %T" -d '1 Jan 2001'
2001-01-01 00:00:00

(Here, that sh goes to $0 and gets only used if the shell needs to print an error message. In any case, we need to pass some value there as $0 is filled before the positional parameters.)

Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441
Loading