All Questions
Tagged with variable-substitution parameter
10 questions
1
vote
1
answer
185
views
Zsh: How to make `${x:?}` return non-zero in case of empty `$x`?
How to make ${x:?} return non-zero in case of empty $x? By default, it exits the shell (subshell in this example) with 0.
function sth() {
local url="${1:?}"
...
}
(sth) ; echo $? # ...
10
votes
5
answers
7k
views
Using parameter substitution on a Bash array
I have file.txt that I need to read into a Bash array. Then I need to remove spaces, double quotes and all but the first comma in every entry. Here's how far I've gotten:
$ cat file.txt
10,this
2 0 ...
12
votes
10
answers
16k
views
Bash: Extract one of the four sections of an IPv4 address
We can use the syntax ${var##pattern} and ${var%%pattern} to extract the last and first section of an IPv4 address:
IP=109.96.77.15
echo IP: $IP
echo 'Extract the first section using ${var%%pattern}: ...
7
votes
1
answer
1k
views
Word splitting in positional parameters
Consider the following example:
IFS=:
x="a :b" # three spaces
echo ["$x"] # no word splitting
# [a :b] # as is
echo [$x] # word splitting
# [a b] # four spaces
Word splitting ...
1
vote
2
answers
2k
views
what does it mean shell read command line arguments ${1,,}
In the Shell script code, the command line arguments assigned to variable like below. what does it mean comma(,) in the statement. What will be the difference when comma is added twice while reading ...
2
votes
2
answers
14k
views
How to assign value to input variable in shell
I am asking user for input and taking input in the variables such as $1, $2 etc.
I am checking the variable for null value and wants to replace the value if null.
if [ "$2" == "" ]; then
2=value
fi
...
5
votes
2
answers
5k
views
Print file name extension using -exec in find
I am playing around with -exec flag of find command. I am trying to use the flag to print the extension name of files, using a fairly new Linux distribution release.
Starting simple, this works:
...
3
votes
2
answers
1k
views
Why does $var give the value of $0?
I have written a very simple awk script that reverses every word of a file-:
awk '
{
for (i=1;i<=NF;i++)
{ x=""
for(j=length($i);j>0;j--)
x=x substr($0,j,1);
print x
}}' file1
The contents of ...
2
votes
2
answers
349
views
Parameter substitution and error messages: suppressing line numbers etc
Here is my example file:
!/bin/bash
# argument-one
# Is first argument missing?
# First method
[[ "$1" == "" ]] && echo "Usage: $(basename $0) filename"; exit 1
# Second method
# filename=$...
0
votes
1
answer
125
views
Special Parameter query - multiple used to obtain command name? [duplicate]
In a uni text provided us to cover bash scripting, the following variable assignment has got be stumped and I've yet to get an answer back from anyone, hence hopefully someone on here can help.
name=$...