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

i want to change the names of the files and this change is in the middle. i basically need to remove a part of the variable and write something else

i have tried this $ls casts.c endian.c ptr.c signed-unsigned-representations.c signed-unsigned.c test-hard-link.c $for i in *.c;do mv "$i" "$i"__swa.c; done $ls casts.c__swa.c endian.c__swa.c ptr.c__swa.c signed-unsigned-representations.c__swa.c…
suliman
  • 11
  • 3
0
votes
1 answer

Bat/Cmd vs. Sh/Bash arguments "stat"-like expansion

trying to convert an old bat (batch) file of mine into sh (bash) yet there are a few things that I still have trouble finding the sh equivalent (for delims tokens being one). Most notably though, the pretty nifty parameter expansion found in bat…
Kochise
  • 504
  • 5
  • 10
0
votes
1 answer

bash scription if conditional "${1:-}"

I try to understant the if condition log_daemon_msg () { if [ -z "${1:-}" ]; then return 1 fi log_daemon_msg_pre "$@" if [ -z "${2:-}" ]; then echo -n "$1:" || true return fi echo -n "$1: $2" ||…
looock7897
  • 27
  • 4
0
votes
1 answer

How to use parameter expansion correctly in bash

I have a string with the structure task_name-student_name and I want to split it into two variables: task: containing the chunk before the - student: containing the chunk after the - I can get this to work with…
James Newton
  • 6,623
  • 8
  • 49
  • 113
0
votes
2 answers

Prevent variable expansion in the parameter of a function

function kubeall { for i in `seq 0 2`; do echo pod-$i kubectl exec -it pod-$i -- bash -c "$@" done } kubeall "cat ~/logs/pod-$i/log.out" Is it possible to prevent expansion of the variable ($i in this case) in the…
subtleseeker
  • 4,415
  • 5
  • 29
  • 41
0
votes
1 answer

bash parameter transformation gives bad substitution

I always get a bad substitution error when I use Parameter Transformation in a script #!/usr/bin/env bash abc=abc echo ${abc@U} result line 3: ${abc#U}: bad substitution However, it works fine in interactive mode > abc=abc > echo ${abc@U} ABC I'm…
William Rusnack
  • 908
  • 8
  • 15
0
votes
1 answer

How to replace the last octet in an IP adress using Bash?

I have a variable that contains an IP address. ip=192.165.12.30 How can I replace the last octet with 0 so that it becomes as below? ip=192.168.12.0
lystack
  • 41
  • 4
0
votes
1 answer

Different behaviour with echo vs printf command line arguments in zsh

I have a simple program to print the command line arguments in zsh. However, I see different behaviour with printf vs echo. Can anyone explain? #!/bin/zsh echo "$# @ : "$@" " printf "\n" printf "$# * $* " printf "\n" printf "$# @ : "$@"…
0
votes
1 answer

Bad subsitution when trying to do string replacement with parameter expansion

Maybe this is a dumb question, but I'm writing a very small Gedit external tool for compiling .qrc (Qt resource files) on a key press. This is my code: echo $GEDIT_CURRENT_DOCUMENT_PATH pyrcc $GEDIT_CURRENT_DOCUMENT_PATH -o…
TheEagle
  • 5,808
  • 3
  • 11
  • 39
0
votes
0 answers

How can I override a bash variable at the time of executing it in console?

I have a bash script that will print yesterdays date. It looks like this: #!/bin/bash YESTERDAY_DATE=$(date '+%Y-%m-%d' -d '1 day ago') echo $YESTERDAY_DATE and I can run it like: ./script.sh, output as follows: 2021-02-23 Now, I want to be…
Saffik
  • 911
  • 4
  • 19
  • 45
0
votes
1 answer

What does `##*/` means in bash `${qidpath##*/}`?

I try to contribute to a Wikimedia opensource project but I don't get what ##*/ means in bash ${qidpath##*/}. Searching on google for ##*/ is chaotic. /tmp/datasets/raw/* contains a list of folders. for qidpath in /tmp/datasets/raw/*; do …
Hugolpz
  • 17,296
  • 26
  • 100
  • 187
0
votes
1 answer

Redundant use of :- in Bash?

I have this code inside a function: local var="$1" local fileVar="${var}_FILE" local def="${2:-}" if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then echo >&2 "error: both $var and $fileVar are set (but are exclusive)" exit 1 fi What is the…
codeforester
  • 39,467
  • 16
  • 112
  • 140
0
votes
0 answers

Parameter expansion with cron

My script.sh contains parameter expansion something like this variable=${variable/PUT_CONTENT/$data} This should replace the substring PUT_CONTENT with variable data. If I run the script manually in terminal like this: ./script.sh everything works…
S3jp4kCZE
  • 15
  • 6
0
votes
1 answer

Tar multiple files in for loop

I have this loop which loops through directories and looks for specific files ($path is read from script prompt): for FILE in $path/*/logs/*2016-*; do ######note that I don't have quotes around $path/*/logs/*2016-* in order for parameter expansion…
TheNewGuy
  • 559
  • 1
  • 10
  • 27
0
votes
1 answer

Bash Parameter Expansion - get part of directory path

Lets say I have this variable FILE=/data/tenant/dummy/TEST/logs/2020-02-03_16.20-LSTRM.log I'm trying to get the name of the 4th sub directory, in this example TEST job=${FILE%/*/*} # This gives me /data/tenant/dummy/TEST Then name=${job##*/}…
TheNewGuy
  • 559
  • 1
  • 10
  • 27