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
2 answers

Shell script variable expansion with escape

The following script: #!/bin/bash nested_func() { echo $1 echo $2 } func() { echo $1 nested_func $2 } func 1 "2 '3.1 3.2'" Outputs: 1 2 '3.1 What I would like to get as output is: 1 2 3.1 3.2 How do I achieve this output with a…
0
votes
2 answers

Bash pattern matching 'or' using shell parameter expansion

hashrate=${line//*:/} hashrate=${hashrate//H\/s/} I'm trying to unify this regex replace into a single command, something like: hashrate=${line//*:\+/H\/s/} However, this last option doesn't work. I also tried with \|, but it doesn't seem to work…
aegroto
  • 43
  • 3
0
votes
1 answer

How do you strip everything after a pattern in a filename?

Edit: I simply need to strip the .gz SUBMIT joust_aallgeier_attempt_2015-11-12-20-37-40_project04.tar.gz These are gunzipped already. I need project.tar output I have the following stripping the first portion. filename=${j##*_}
M2bandit
  • 99
  • 1
  • 9
0
votes
1 answer

OSX variable assignments giving error

How is possible that 'delta_oligo_combined.bedgraph' file is in the directory and can't be seen by cat command? OSX typo? $ type=oligo $ file_type=combined $ ls delta_oligo_combined.bedgraph delta_oligo_combined.bedgraph $ cat…
biotech
  • 697
  • 1
  • 7
  • 17
0
votes
3 answers

Bash bad substitution with subshell

I am trying to get the Distro name without the quotation marks. cat /etc/*-release | grep "NAME=.*" -o | cut -d "=" -f2 | head -n1 Returns: "CentOS Linux" Now, using shell substitution, I tried to remove the quotation marks my placing the command in…
ChaChaPoly
  • 1,811
  • 5
  • 17
  • 39
0
votes
1 answer

In bash, what does dir=${0%/*} means?

I have found this piece of code while studying a bash script: dir=${0%/*} I suspect the code inside the braces to be regex but I don't see what it means. Any idea?
ahg8tOPk78
  • 317
  • 1
  • 2
  • 8
0
votes
2 answers

Bash expr command

I am trying to make a bash shell script that can add a name value pair to a text file, for example TEST=true. I am trying to make it so if the user tries to add a name that already exists for example TEST=false it does not let them to do it. Can…
mrblippy
  • 311
  • 2
  • 3
  • 10
-1
votes
0 answers

Shell parameter expansion not working in regex to "rename"

I'm trying to rename files in folders for a bioinformatics project. There is a folder for each sample which is named, the files in the folders all have the same names which is no good for downstream processing so I am trying to rename them with a…
-1
votes
1 answer

How to split a word in the given positive or negative fraction

So for example I have a word: swimming and running. Swimming consist of 8 letters and running of seven, if there is a given fraction, like 3/4 for swimming and -2/7 for running. It would need to give me swimmi and ng because the fraction given to…
GJO
  • 11
  • 5
-1
votes
3 answers

Separating integer and fractional part of a decimal number

I want to separate a numeric decimal number, storing the integer part in bash variable ts and the fractional part in variable fr. Had a go with parameter expansion, but keep getting the original number. t=13.491675 ts=${t#![0-9]*} fr=${t%*![0-9]}
Han
  • 15
  • 4
-1
votes
1 answer

Bash Parameter Expansion: What does the Dollar Sign ($) mean in the pattern?

Reference: How to remove a newline from a string in Bash I feel a little silly asking this, but when I'm solving a problem, I like to learn about the WHY instead of just copying-and-pasting code I found on the Internet. :-) I've been reading this…
Hossy
  • 139
  • 1
  • 2
  • 11
-1
votes
1 answer

"${line//END}" != "$line" - what does it do

So i have the result of an API call to a file containing PEM certificates (many of them) like this: -----BEGIN…
trustbyte
  • 21
  • 1
  • 6
-1
votes
1 answer

What does the statement, f2=${f%????} , mean?

I'm trying to understand what the expression, f2=${f%????} means in a bash script. I tried searching the web for some reference, but no luck finding something useful. The code I'm using is: for f in "$@" do f2=${f%????} /usr/bin/openssl smime -in…
lgheorghe
  • 3
  • 3
-1
votes
1 answer

launch an exe with parameter from python

I'm trying to call an application and pass a text file as a parameter. The text file has instructions. I tried passing a parameter when opening notepad but couldn't get it to work. Here is the code I'm trying: import…
-1
votes
1 answer

How to understand this "bash" shell command

The command is: [ -d $x ] && echo $x | grep "${1:-.*}" I have run it separately, and [ -d $x ] && echo $x just outputs the directory name. What does the ${1:-.*} mean?
Tong
  • 83
  • 5
1 2 3
11
12