Suppose you have the following script:
#!/usr/bin/ksh
for element in $#
do
echo $element
done
And you execute the script using:
./myscript.ksh "my element"
The expected output should be:
my element
but the whitespace is treated as a separator for each argument, obtaining:
my
element
How should I escape the whitespace?
Thanks
You must enclose $#, see Special Parameters, in double quotes:
($#) Expands to the positional parameters, starting from one.
...
When the expansion occurs within double quotes, and word splitting
is performed, each parameter expands to a separate word. That is,
"$#" is equivalent to "$1" "$2" ….
#!/usr/bin/ksh
for element in "$#"
do
echo $element
done
You can get the same output by omitting in "$#" (Looping Constructs, for) altogether:
If ‘in words’ is not present, the for command executes the commands
once for each positional parameter that is set, as if ‘in "$#"’ had
been specified
#!/usr/bin/ksh
for element
do
echo $element
done
You must either quote the $#, or use nothing:
for element in "$#"
or
for element
will both work.
Related
How to add suffix and prefix to $#?
If I do $PREFIX/$#/$SUFFIX, I get the prefix and the suffix only in the first parameter.
I would use shell [ parameter expansion ] for this
$ set -- one two three
$ echo "$#"
one two three
$ set -- "${#/#/pre}" && set -- "${#/%/post}"
$ echo "$#"
preonepost pretwopost prethreepost
Notes
The # matches the beginning
The % matches the end
Using double quotes around ${#} considers each element as a separate word. so replacement happens for every positional parameter
Let's create a parameters for test purposes:
$ set -- one two three
$ echo "$#"
one two three
Now, let's use bash to add prefixes and suffixes:
$ IFS=$'\n' a=($(printf "pre/%s/post\n" "$#"))
$ set -- "${a[#]}"
$ echo -- "$#"
pre/one/post pre/two/post pre/three/post
Limitations: (a) since this uses newline-separated strings, it won't work if your $# contains newlines itself. In that case, there may be another choice for IFS that would suffice. (b) This is subject to globbing. If either of these is an issue, see the more general solution below.
On the other hand, if the positional parameters do not contain whitespace, then no change to IFS is needed.
Also, if IFS is changed, then one may want to save IFS beforehand and restore afterward.
More general solution
If we don't want to make any assumptions about whitespace, we can modify "$#" with a loop:
$ a=(); for p in "$#"; do a+=("pre/$p/post"); done
$ set -- "${a[#]}"
$ echo "$#"
pre/one/post pre/two/post pre/three/post
Note: This is essentially a slightly more detailed version of sjam's answer.
John1024's answer is helpful, but:
requires a subshell (which involves a child process)
can result in unwanted globbing applied to the array elements.
Fortunately, Bash parameter expansion can be applied to arrays too, which avoids these issues:
set -- 'one' 'two' # sample input array, which will be reflected in $#
# Copy $# to new array ${a[#]}, adding a prefix to each element.
# `/#` replaces the string that follows, up to the next `/`,
# at the *start* of each element.
# In the absence of a string, the replacement string following
# the second `/` is unconditionally placed *before* each element.
a=( "${#/#/PREFIX}" )
# Add a suffix to each element of the resulting array ${a[#]}.
# `/%` replaces the string that follows, up to the next `/`,
# at the *end* of each element.
# In the absence of a string, the replacement string following
# the second `/` is unconditionally placed *after* each element.
a=( "${a[#]/%/SUFFIX}" )
# Print the resulting array.
declare -p a
This yields:
declare -a a='([0]="PREFIXoneSUFFIX" [1]="PREFIXtwoSUFFIX")'
Note that double-quoting the array references is crucial to protect their elements from potential word-splitting and globbing (filename expansion) - both of which are instances of shell expansions.
I'm trying to create a shell script that sets up my Ubuntu server for a Laravel app. The user is asked to confirm before proceeding with the following code taken from here:
How do I prompt a user for confirmation in bash script?
#!/bin/sh
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
echo "\n ${GREEN}Enter the folder name for the Laravel application: ${NC}"
read APP_NAME
read -r -p "Are you sure? [y/N] " response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y)$ ]]
then
echo "Installing dependencies..."
else
exit
fi
I'm getting this error:
Bad substitution
on the line
response=${response,,} # tolower
This is a case modification substitution. Here is the description (from the Bash manual on shell parameter expansion):
${parameter^pattern}
${parameter^^pattern}
${parameter,pattern}
${parameter,,pattern}
This expansion modifies the case of alphabetic characters in
parameter. The pattern is expanded to produce a pattern just as in
filename expansion. Each character in the expanded value of parameter
is tested against pattern, and, if it matches the pattern, its case is
converted. The pattern should not attempt to match more than one
character. The ‘^’ operator converts lowercase letters matching
pattern to uppercase; the ‘,’ operator converts matching uppercase
letters to lowercase. The ‘^^’ and ‘,,’ expansions convert each
matched character in the expanded value; the ‘^’ and ‘,’ expansions
match and convert only the first character in the expanded value. If
pattern is omitted, it is treated like a ‘?’, which matches every
character. If parameter is ‘#’ or ‘*’, the case modification operation
is applied to each positional parameter in turn, and the expansion is
the resultant list. If parameter is an array variable subscripted with
‘#’ or ‘*’, the case modification operation is applied to each member
of the array in turn, and the expansion is the resultant list.
This works on bash >= 4.0.
Alternatively, you can use
response=$(echo "$response" | tr '[:upper:]' '[:lower:]')
Thanks to David C. Rankins help in the comments section this issue was resolved by changing:
#!/bin/sh
to
#!/bin/bash
and changing
echo "string data"
to
echo -e "string data"
I'm wondering how I can pass a string containing spaces to Zenity for the text argument as my current method is truncating/failed to evaluate all the text after the first space.
Here is an MVP which shows the issue:
Script
#!/bin/bash
a="test test test"
test_func() {
echo "$#"
$(zenity --info --text "test test")
$(zenity --info --text "$#")
}
test_func ${a}
Output
$> test test test
$> (zenity info window with test test as text)
$> (zenity info window with test) *** should contain "test test test"
Use "$*" instead of "$#".
Manual for "$#" (emphasis mine):
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$#" is equivalent to "$1" "$2" ... If the double-quoted expan sion occurs within a word, the expansion of the first parameter is joined with the beginning part of the orig‐ inal word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$#" and $# expand to nothing (i.e., they are removed).
And for "$*":
Expands to the positional parameters, starting from one. When the expansion is not within double quotes, each positional parameter expands to a separate word. In contexts where it is performed, those words are subject to further word splitting and pathname expansion. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
Alternatively, you could quote ${a} to pass a as a single argument instead of splitting on spaces.
What does $# in unix shell script signify. For example:
A__JOB="$CLASS $#"
where $CLASS has my java class file name. So what might be the meaning of
$#.
What did I do?
I Googled :) but $# seems to be complex query for ir or maybe i do not know how to search google for special characters.
$# is the value of all arguments passed.
For example, if you pass:
./script A B C D
then "$#" will be equal to "A" "B" "C" "D"
So it looks like the purpose is to passe all the arguments passed to the script directly to the java program.
From bash manual:
# Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, each parameter
expands to a separate word. That is, "$#" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs
within a
word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the
last parameter is joined with the last part of the original word. When there are no positional parameters, "$#" and $# expand
to nothing (i.e., they are removed).
What does a dollar sign followed by an at-sign (#) mean in a shell script?
For example:
umbrella_corp_options $#
$# is all of the parameters passed to the script.
For instance, if you call ./someScript.sh foo bar then $# will be equal to foo bar.
If you do:
./someScript.sh foo bar
and then inside someScript.sh reference:
umbrella_corp_options "$#"
this will be passed to umbrella_corp_options with each individual parameter enclosed in double quotes, allowing to take parameters with blank space from the caller and pass them on.
$# is nearly the same as $*, both meaning "all command line arguments". They are often used to simply pass all arguments to another program (thus forming a wrapper around that other program).
The difference between the two syntaxes shows up when you have an argument with spaces in it (e.g.) and put $# in double quotes:
wrappedProgram "$#"
# ^^^ this is correct and will hand over all arguments in the way
# we received them, i. e. as several arguments, each of them
# containing all the spaces and other uglinesses they have.
wrappedProgram "$*"
# ^^^ this will hand over exactly one argument, containing all
# original arguments, separated by single spaces.
wrappedProgram $*
# ^^^ this will join all arguments by single spaces as well and
# will then split the string as the shell does on the command
# line, thus it will split an argument containing spaces into
# several arguments.
Example: Calling
wrapper "one two three" four five "six seven"
will result in:
"$#": wrappedProgram "one two three" four five "six seven"
"$*": wrappedProgram "one two three four five six seven"
^^^^ These spaces are part of the first
argument and are not changed.
$*: wrappedProgram one two three four five six seven
These are the command line arguments where:
$# = stores all the arguments in a list of string
$* = stores all the arguments as a single string
$# = stores the number of arguments
The usage of a pure $# means in most cases "hurt the programmer as hard as you can", because in most cases it leads to problems with word separation and with spaces and other characters in arguments.
In (guessed) 99% of all cases, it is required to enclose it in ": "$#" is what can be used to reliably iterate over the arguments.
for a in "$#"; do something_with "$a"; done
Meaning.
In brief, $# expands to the arguments passed from the caller to a function or a script. Its meaning is context-dependent: Inside a function, it expands to the arguments passed to such function. If used in a script (outside a function), it expands to the arguments passed to such script.
$ cat my-script
#! /bin/sh
echo "$#"
$ ./my-script "Hi!"
Hi!
$ put () { echo "$#"; }
$ put "Hi!"
Hi!
* Note: Word splitting.
The shell splits tokens based on the contents of the IFS environment variable. Its default value is \t\n; i.e., whitespace, tab, and newline. Expanding "$#" gives you a pristine copy of the arguments passed. Expanding $# may not. More specifically, any arguments containing characters present in IFS might split into two or more arguments or get truncated.
Thus, most of the time what you will want to use is "$#", not $#.
From the manual:
#
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$#" is equivalent to "$1" "$2" .... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$#" and $# expand to nothing (i.e., they are removed).
$# is basically use for refers all the command-line arguments of shell-script.
$1 , $2 , $3 refer to the first command-line argument, the second command-line argument, third argument.
They are often used to simply pass all arguments to another program
[root#node1 shell]# ./my-script hi 11 33
hi 11 33
[root#node1