Reading arguments in shell script - bash

Very simple question, but I have very little knowledge of shell script. What does the following command mean, when reading the arguments that were passed to the script?
TXT=${1,,}

It converts a variable to lower case.
Example:
$ var="Hello World"
$ echo ${var,,}
hello world
In your case, $1 refers to the first argument passed to your shell script, so TXT=${1,,} converts the first argument to lower case and stores it in another variable called TXT.
Type man bash and you will find the following detailed explanation of this expression:
${parameter,,pattern}
Case modification. This expansion modifies the case of alphabetic char-
acters in parameter. The pattern is expanded to produce a pattern just
as in pathname expansion. The ^ operator converts lowercase letters
matching pattern to uppercase; the , operator converts matching upper-
case 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 mod-
ification operation is applied to each member of the array in turn, and
the expansion is the resultant list.

Related

what does "// /_" mean in bash scripting [duplicate]

This question already has an answer here:
What is the meaning of `//` in Bash parameter expansions?
(1 answer)
Closed last year.
I am currently moving our shell/bash scripts from a jenkinsfile to groovy scripts that are stored in methods but still execute as sh scripts.
The issue i have is with variables containing // /_
exmaple:
${VARIABLE_NAME// /_}
I cannot find what // /_ exactly does when supplied like this in a variable.
I need to find another way to do this because when moved to Groovy methods, it causes formating issues where escaping doesnt work properly.
It will replace all spaces with underscores.
Consider the following example:
$ var='hello world john doe'
$ echo "${var// /_}"
hello_world_john_doe
$
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. The match is performed according to the rules described below (see Pattern Matching). If pattern begins with ‘/’, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with ‘#’, it must match at the beginning of the expanded value of parameter. If pattern begins with ‘%’, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If the nocasematch shell option (see the description of shopt in The Shopt Builtin) is enabled, the match is performed without regard to the case of alphabetic characters. If parameter is ‘#’ or ‘’, the substitution 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 substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.
Copied from: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html with my emphasize on how / and // differences.

bash shell reworking variable replace dots by underscore

I can't see to get it working :
echo $VERSIONNUMBER
i get : v0.9.3-beta
VERSIONNUMBERNAME=${VERSIONNUMBER:1}
echo $VERSIONNUMBERNAME
I get : 0.9.3-beta
VERSION=${VERSIONNUMBERNAME/./_}
echo $VERSION
I get : 0_9.3-beta
I want to have : 0_9_3-beta
I've been googling my brains out I can't make heads or tails of it.
Ideally I'd like to remove the v and replace the periods with underscores in one line.
Let's create your variables:
$ VERSIONNUMBER=v0.9.3-beta
$ VERSIONNUMBERNAME=${VERSIONNUMBER:1}
This form only replaces the first occurrence of .:
$ echo "${VERSIONNUMBERNAME/./_}"
0_9.3-beta
To replace all occurrences of ., use:
$ echo "${VERSIONNUMBERNAME//./_}"
0_9_3-beta
Because this approach avoids the creation of pipelines and subshells and the use of external executables, this approach is efficient. This approach is also unicode-safe.
Documentation
From man bash:
${parameter/pattern/string}
Pattern substitution. The pattern is expanded to produce a pattern
just as in pathname expansion. Parameter is expanded and the longest
match of pattern against its value is replaced with
string. If pattern begins with /, all matches of pattern are replaced
with string. Normally only the first match is replaced. If
pattern begins with #, it must match at the beginning of the expanded
value of parameter. If pattern begins with %, it must match at the
end of the expanded value of parameter. If string
is null, matches of pattern are deleted and the / following pattern
may be omitted. If the nocasematch shell option is enabled, the
match is performed without regard to the case of alphabetic
characters. If parameter is # or *, the substitution 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 substitution operation is
applied to each member of the array in turn, and the expansion is the
resultant list.
(Emphasis added.)
You can combine pattern substitution with tr:
VERSION=$( echo ${VERSIONNUMBER:1} | tr '.' '_' )

What is the meaning of `//` in Bash parameter expansions?

What does //,/ mean in this command?
echo ${foo//,/}
From the bash(1) man page (http://linux.die.net/man/1/bash):
${parameter/pattern/string}
Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is # or *, the substitution 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 substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.
That is, ${something//,/} is expanded to the $something with all the occurrences of , removed.

Stripping characters from a Bash variable

I am attempting to write a bash script (poorly) and need assistance in stripping characters from a variable.
variable is defined as $managementipmask= 111.111.111.111/24
I need to strip the /24 from the end of the variable.
Thanks in advance.
Use parameter expansion to remove everything from the first /:
$ k="111.111.111.111/24"
$ echo "${k%%/*}"
111.111.111.111
See this resource on parameter expansion for additional details:
http://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion
${parameter%pattern}
The 'pattern' is matched against the end of 'parameter'. The result is
the expanded value of 'parameter' with the shortest match deleted.
${parameter%%pattern}
As above, but the longest match is deleted.
So you can delete from the last / using a single %:
$ k="111.111.111.111/24/23"
$ echo "${k%/*}"
111.111.111.111/24
Another way:
k="111.111.111.111/24"
echo "${k/%\/24/}"
It replaces last /24 with empty string.
From Bash Manual:
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern
against its value is replaced with string. If pattern begins with ‘/’,
all matches of pattern are replaced with string. Normally only the
first match is replaced. If pattern begins with ‘#’, it must match at
the beginning of the expanded value of parameter. If pattern begins
with ‘%’, it must match at the end of the expanded value of parameter.
If string is null, matches of pattern are deleted and the / following
pattern may be omitted. If parameter is ‘#’ or ‘’, the substitution
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 substitution operation is applied to
each member of the array in turn, and the expansion is the resultant
list.

clarification with a shell script

Can somebody explain how echo "${PWD/#$HOME/~}" would print ~ in case the PWD evaluates to $HOME. Never read about such replacement using echo. What is going on here?
It is not echo it is your shell makes Parameter Expansion using ${parameter/pattern/string} syntax:
The pattern is expanded to produce a pattern just as in filename
expansion. Parameter is expanded and the longest match of pattern
against its value is replaced with string. If pattern begins with ‘/’,
all matches of pattern are replaced with string. Normally only the
first match is replaced. If pattern begins with ‘#’, it must match at
the beginning of the expanded value of parameter. If pattern begins
with ‘%’, it must match at the end of the expanded value of parameter.
If string is null, matches of pattern are deleted and the / following
pattern may be omitted. If parameter is ‘#’ or ‘*’, the substitution
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 substitution operation is applied to
each member of the array in turn, and the expansion is the resultant
list.
It doesn't look like POSIX supports it.
In your case, it replaces the value of $HOME envvar (not the string '$HOME' literally) with ~ in the output if PWD envvar starts with it.

Resources