Questions tagged [parameter-expansion]

Parameter expansion replaces variables with their values as an evaluation phase of a command in most Bourne-derived shells.

Parameter expansion is one of the series of transformations that commands go through when evaluated in a shell.

Its main function is to interpolate parameters, a.k.a. shell variables, but it also allows for some manipulation. For example, in bash, assuming variable var contains the string 1234abcd:

  • $var expands to 1234abcd
  • ${#var} expands to the parameter's length, 8
  • ${var%cd} expands to the parameter tail-trimmed of cd: 1234ab

Section parameter expansion from bash's Reference Manual

167 questions
0
votes
1 answer

Anyone can explain to me ip="${ip:-${default_ip}}

I need some help from you guys to understand this black of code. I am having a problem with the last line. thanks default_ip=$(hostname -I) //display localhost ip? printf Put your local IP //print statement read ip …
user12098281
0
votes
0 answers

What does ${VAR_NAME+x} means in bash/shell?

I have an expression -z ${VAR_NAME+x} which I know will check if ${VAR_NAME+x} is an empty string. But I can't find out what is the purpose of having the +x.
Kevin Winata
  • 443
  • 2
  • 10
0
votes
0 answers

How to get the last field and pass it on?

I've try next script #!/bin/bash VERSION="2.0.3.44" BUILD_NUMBER=${VERSION##*.} BUILD_QUERY="${BUILD_NUMBER}/foo/bar/" Expected result: 44/foo/bar/ Actual result: /foo/bar/ Has anyone come across this?
Alesia
  • 49
  • 1
  • 4
0
votes
3 answers

What is the meaning of opts=${1:+--host $1} in bash?

In a bash script, I stumbled upon this piece of code: opts=${1:+--host $1} /path/somecmd $opts somesubcmd By testing, I found out that $opts expands to --host $1 whatever $1 is. I wonder what is the purpose of this syntax. Why not simply use…
LuoLeKe
  • 95
  • 1
  • 8
0
votes
2 answers

Replication and expansion of program flags in BASH script

I am working with a program that combines individuals files, and I am incorporating this program into a BASH pipeline that I'm putting together. The program requires a flag for each file, like so: program -V file_1.g.vcf -V file_2.g.vcf -V…
BTS
  • 13
  • 2
0
votes
2 answers

Compile query from raw string (without using .text(...)) using Sqlalchemy connection and Postgres

I am using Sqlalchemy 1.3 to connect to a PostgreSQL 9.6 database (through Psycopg). I have a very, very raw Sql string formatted using Psycopg2 syntax which I can not modify because of some legacy issues: statement_str = SELECT * FROM users WHERE…
Savir
  • 17,568
  • 15
  • 82
  • 136
0
votes
2 answers

Value of var is not shown when concatenating $ and var --> $var

I've got about a day of experience in bash as of now.. string () { for (( i=0; i<${#1}; i++ )) do echo "$"${1:$i:1}"" done } string "hello" This script returns "$h", "$e", "$l", "$l", "$o", but I actually want it to return the…
Cedric
  • 245
  • 1
  • 12
0
votes
1 answer

double quotes in zsh parameter expansion

I have two test cases in zsh A. without quotes ~$ y=(${(f)$(echo -e "a b\nc d")}); printf "<%s>\n" "${y[@]}" B. with quotes ~$ y=(${(f)"$(echo -e "a b\nc d")"}); printf "<%s>\n" "${y[@]}" However if I first assign the…
doraemon
  • 2,296
  • 1
  • 17
  • 36
0
votes
2 answers

/bin/dash: Bad substitution

I need to do a string manipuilation in shell script (/bin/dash): #!/bin/sh PORT="-p7777" echo $PORT echo ${PORT/p/P} the last echo fails with Bad substitution. When I change shell to bash, it works: #!/bin/bash PORT="-p7777" echo $PORT echo…
Martin Vegter
  • 136
  • 9
  • 32
  • 56
0
votes
1 answer

renaming series of files using xargs

I would like to rename several files picked by find in some directory, then use xargs and mv to rename the files, with parameter expansion. However, it did not work... example: mkdir test touch abc.txt touch def.txt find . -type f -print0 | \ xargs…
Oliver
  • 147
  • 9
0
votes
0 answers

Why Parameter Expansion is not working?

I am writing a script and in between. I am trying to replace all the commas of the string stored in a variable to spaces. basically the variable dpoint contains comma saparated floating point numbers like 0.0,4.2,3.6666 i am using…
sanjeevprasad
  • 804
  • 1
  • 6
  • 21
0
votes
0 answers

How to escape bash parameter expansion (when a "!" followed by letters is a parameter)

I have a bash script I need to call with a string parameter, which string starts with a exclamation mark "!" (this is mandatory in what I'm doing): myCommand.sh "!HELLO" Because of parameter expansion, bash reports an error: -bash: !HELLO: event…
Bob
  • 1,495
  • 1
  • 19
  • 24
0
votes
1 answer

How to pass a file with space in the name to command FC?

I have a diff script that wraps the command FC so that I just have to drop two files on it: FC /L /N "%1" "%2" > diff.out.txt 2> diff.err.txt It works fine, but if one of the files has spaces in the name... For example, my file paths being…
joH1
  • 555
  • 1
  • 9
  • 27
0
votes
2 answers

Parameter expansion without colon (default value to empty string) in a condition

I know the difference between ${var:-word} and ${var-word}, but I don't see the purpose of using a default value in this condition: [ -z "${NVM_PROFILE-}" ] Why is an empty default value needed there? Is it something related to compatibility with…
whoan
  • 8,143
  • 4
  • 39
  • 48
0
votes
0 answers

Bash: declare array dynamically based on another array, while keeping element structure

I have a script that can be run with different "flavors". The logic for each flavor is almost identical (except for the values for variables it uses). So I decided to declare all variables for each flavor, using a namespace pattern (i.e.…