parameter-expansion

How can I just extract one underbar-separated field from a filename?

北慕城南 提交于 2021-02-05 10:48:07
问题 I have a list of file names like this: REG_2016120200hourly_d01_20161202_00_00_00.nc Of this name I would like to extract and put in a variable: 1)date 20161202 for file in /path/*; do filename=$(basename -- "$file") date=${filename:4:8} echo $date done and this is working, the script give me 20161202 and I don't know why 2)timestep 00 I need to take the firsts two zero 00 and I'm trying with timestep=${filename:34:36} but this doesn't work. I'm a little bit surprised because I used the same

Shell parameter expansion on arrays

橙三吉。 提交于 2019-12-17 21:29:04
问题 Say I read some data into a Bash array: $ IFS=" " read -a arr <<< "hello/how are/you iam/fine/yeah" Now, I want to print the first / -sliced field for each element in the array. What I do is to loop over the elements and use shell parameter expansion to strip everything from the first / : $ for w in "${arr[@]}"; do echo "${w%%/*}"; done hello are iam However, since printf allows us to print the whole content of the array in a single expression: $ printf "%s\n" "${arr[@]}" hello/how are/you

Bash 4.4 prompt escape for number of jobs currently running

天大地大妈咪最大 提交于 2019-12-11 23:13:54
问题 I stumbled across this post where user chepner proposed in his answer the usage of \j (as mentioned in the bash manual) to retrieve the current running count of background jobs. Basically it boils down to num_jobs="\j" echo ${num_jobs@P} Can anyone enlighten me on what is going on here exactly? E.g. why ${\j@P} is not working and what @P is doing exactly? 回答1: Like any parameter expansion, you have to supply the name of a parameter, not an arbitrary string. \j isn't the name of a parameter;

In Bash, is there a way to expand variables twice in double quotes?

扶醉桌前 提交于 2019-12-06 06:33:15
问题 For debugging my scripts, I would like to add the internal variables $FUNCNAME and $LINENO at the beginning of each of my outputs, so I know what function and line number the output occurs on. foo(){ local bar="something" echo "$FUNCNAME $LINENO: I just set bar to $bar" } But since there will be many debugging outputs, it would be cleaner if I could do something like the following: foo(){ local trace='$FUNCNAME $LINENO' local bar="something" echo "$trace: I just set bar to $bar" } But the

In Bash, is there a way to expand variables twice in double quotes?

拥有回忆 提交于 2019-12-04 14:54:33
For debugging my scripts, I would like to add the internal variables $FUNCNAME and $LINENO at the beginning of each of my outputs, so I know what function and line number the output occurs on. foo(){ local bar="something" echo "$FUNCNAME $LINENO: I just set bar to $bar" } But since there will be many debugging outputs, it would be cleaner if I could do something like the following: foo(){ local trace='$FUNCNAME $LINENO' local bar="something" echo "$trace: I just set bar to $bar" } But the above literally outputs: "$FUNCNAME $LINENO: I just set bar to something" I think it does this because

Bash: How to use operator parameter expansion ${parameter@operator}?

北城以北 提交于 2019-11-28 07:21:47
问题 I've googled and tried so many things and never could get anything to work with ${parameter@operator}. All I find is more links to the same documentation. So I think a proper answer with practical examples would be very helpful to its understanding. The documentation says: ${parameter@operator} The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter: Q The expansion is a

How to execute command stored in a variable?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 19:36:34
What is the correct way to call some command stored in variable? Is there any differences between 1 and 2? #!/bin/sh cmd="ls -la $APPROOTDIR | grep exception" #1 $cmd #2 eval "$cmd" Unix shells operate a series of transformations on each line of input before executing them. For most shells it looks something like this (taken from the bash manpage): initial word splitting brace expansion tilde expansion parameter, variable and arithmetic expansion command substitution secondary word splitting path expansion (aka globbing) quote removal Using $cmd directly gets it replaced by your command during

How to execute command stored in a variable?

心不动则不痛 提交于 2019-11-26 07:19:42
问题 What is the correct way to call some command stored in variable? Is there any differences between 1 and 2? #!/bin/sh cmd=\"ls -la $APPROOTDIR | grep exception\" #1 $cmd #2 eval \"$cmd\" 回答1: Unix shells operate a series of transformations on each line of input before executing them. For most shells it looks something like this (taken from the bash manpage): initial word splitting brace expansion tilde expansion parameter, variable and arithmetic expansion command substitution secondary word