What does ${1-1} in bash mean?
This means that if argument 1 (${1}
) is not set, set the value to 1.
See parameter substituiton here.
${parameter-default}, ${parameter:-default}
If parameter not set, use default.
string manipulation in bash ${@:1:$#-1}
That's a parameter expansion that returns all the positional parameters but the last one.
It's based on ${parameter:offset:length}
, where using @
as parameter makes it work on positional parameters. The length $#-1
is the number of positional parameters ($#
) minus one.
Here's a showcase : https://ideone.com/HKcaNj
What does `${1+/$1}` mean in Bash?
The /
makes it look slightly more confusing than it is, but this is just an example of ${foo+bar}
, which expands to bar
if $foo
is set.
In this case, the variable is $1
, the first positional parameter passed to the script or function.
example () {
echo "${1+/$1}"
}
example # outputs nothing
example '' # outputs "/"
example foo # outputs "/foo"
There is a table that summarises these parameter expansions in the spec. The rules for ${parameter+word}
are:
- Set and Not Null: substitute
word
- Set But Null: substitute
word
- Unset: substitute null
So to answer your question directly, FOO=${1+/$1}
assigns /$1
to FOO
is $1
is set, otherwise FOO
is set to null (an empty string).
What does `${1#*=}` mean in Bash?
The snippet ${1#*=}
has nothing to do with associative arrays.* (Bash's syntax is super consistent, and not at all confusing)
This is a pattern match on the value of the first argument (${1}
) of your function or script. Its syntax is
${variable#glob}
where
variable
is any bash variable- glob is any glob pattern (subject to pathname expansion) to match against)
It deletes the shortest match, starting at the beginning of the line.
There is also ##
which deletes the longest match starting from the beginning of the variable, %
, which deletes the shortest match starting from the end, and %%
, which deletes the longest match starting from the end.
So, for example, the following code:
myVar="abc=llamas&disclaimer=true"
echo "${myVar#*=}"
will print llamas&disclaimer=true
to the screen.
On the other hand,
myVar="abc=llamas&disclaimer=true"
echo ${myVar##*=}
will print true
, and
myVar="foobar is bad"
echo "${myVar%%b*}"
will print foo
* This is fully explained in the bash man page; just search for the string ${parameter#word}
to find it
Increment variable value by 1 (shell programming)
You can use an arithmetic expansion like so:
i=$((i+1))
or declare i
as an integer variable and use the +=
operator for incrementing its value.
declare -i i=0
i+=1
or use the ((
construct.
((i++))
Bash: ${string:$i:1} what does this mean?
in bash doing something lik this: ${string:3:1} means: take substring starting from the character at pos 3 (0-based, so the 4th character), and length = 1 character.
for example:
string=abc
then ${string:0:1} equals a
and ${string:2:1} equals c
.
This script takes the value of the variable $i
: so it just takes the character at position $i
.
Shell equality operators (=, ==, -eq)
=
and ==
are for string comparisons-eq
is for numeric comparisons-eq
is in the same family as -lt
, -le
, -gt
, -ge
, and -ne
==
is specific to bash (not present in sh (Bourne shell), ...). Using POSIX =
is preferred for compatibility. In bash the two are equivalent, and in sh =
is the only one that will work.
$ a=foo
$ [ "$a" = foo ]; echo "$?" # POSIX sh
0
$ [ "$a" == foo ]; echo "$?" # bash-specific
0
$ [ "$a" -eq foo ]; echo "$?" # wrong
-bash: [: foo: integer expression expected
2
(Note: make sure to quote the variable expansions. Do not leave out the double-quotes above.)
If you're writing a #!/bin/bash
script then I recommend using [[
instead. The double square-brackets [[...]]
form has more features, a more natural syntax, and fewer gotchas that will trip you up. For example, double quotes are no longer required around $a
:
$ [[ $a == foo ]]; echo "$?" # bash-specific
0
See also:
- What's the difference between [ and [[ in Bash?
What does ${1%..} mean in this shell script?
From the Bash Reference Manual:
${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the
%
case) or the longest matching pattern (the%%
case) deleted. If parameter is@
or*
, the pattern removal 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 pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
So if $1
expands to text.tex
then ${1%.tex}
expands to text
.
Related Topics
Best Text Search Engine for Integrating with Custom Web App
Replace Text Based on a Dictionary
Bash: How to Traverse Directory Structure and Execute Commands
How to Do a One Way Diff in Linux
Compile Linux Kernel (2.6) Module Including Non Kernel Headers
Changing Pecl Installation Directory
How to Join a Thread in Linux Kernel
Shell Command to Update Pom File from a Variable
What Does Version Info in Ldd -V Mean
Git Clone from Linux to Tfs Git Repo
Loading Executable or Executing a Library
Linux Iterate Over Files in Directory
Cron Error with Using Backquotes
How to Copy a File with '$' in Name in Linux
How to Measure the Stack Size of a Process
Lazarus: How to List All the Available Network Connection on a System