Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [bash-expansion]

The tag has no usage guidance.

4
votes
1answer
46 views

Is `~#` a tilde expansion of bash?

Is ~# a tilde expansion of bash? I don't find it in https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html. My question is from https://unix.stackexchange.com/a/506532/674. Thanks. $ ...
0
votes
1answer
29 views

Bash script fails where command line works

I wrote a basic bash function to test for the occurence of a string search argument in another string. Running it from the command line gives expected results. Running the same from a shell script ...
0
votes
3answers
81 views

How to prevent parameter expansion around a variable I want to be resolved?

Edit: Important note before reading further: since Kusalananda's anwser, my question is now quite useless, since he pointed out a bug in another script I used. Now this bug is fixed, the below errors ...
0
votes
0answers
27 views

.desktop launcher of $VISUAL failing

I have built a little launcher editor in a desktop file called "createlauncher.desktop" that opens up a second, read-only desktop launcher file called "SHORTCUT.desktop" in a text editor where a PATH ...
0
votes
2answers
47 views

Is there a standard array name for $@, like ${ARGS[@]}?

I don't expect it to be modifiable. That is, I don't expect it to enable something like ARGS=("${ARGS[@]}"), but only to simplify something like myarray=("${ARGS[@]/--unneeded-argument/}"). Note that ...
1
vote
1answer
31 views

Passing $* with parameters with spaces to a second script

I've got a script that takes a variable number of parameters. I also have a wrapper script where I take the same parameters and add some more. The problem is that when I have params with spaces they ...
0
votes
1answer
119 views

Bash: Error in assigning default value to a variable

Inside my bash script: This works: CWD="${1:-${PWD}}" But, if I replace it with: CWD="${1:=${PWD}}" I get the following error line #: $1: cannot assign in this way Why I can't assign to ${1}?
3
votes
1answer
69 views

Commands Differences Using Quotations (Find)

I was reading the man page of find and I found myself confused with the following commands. What is the difference between one and its corresponding one. What is the difference between the following ...
1
vote
5answers
118 views

How can I expand with brackets but use commas instead of spaces as the separators in the output?

I need to do IFS=",";echo {1..5} so that it can output 1,2,3,4,5 instead of 1 2 3 4 5. How do I make bash echo {1..5} and output the values with a comma?
12
votes
3answers
593 views

Automatic variable expansion inside bash [[ ]] command

When dereferencing a variable in bash, you have to use $ sign. Nevertheless, it seems that the following is working just fine: x=5 [[ x -gt 2 ]] Can anybody explain this? Edit: (more info) What I ...
0
votes
3answers
48 views

`$@` vs. `$*` behavior [duplicate]

The bash manual says: Regarding: $* When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special ...
3
votes
2answers
75 views

Prepare arguments containing quoted string in variable

In a Bash script, I call a program like this in several places: numfmt --suffix=" B" --grouping 231210893 Where the number is different every time, but the other parameters stay the same. I would ...
0
votes
2answers
44 views

Restricting Bash Filename Expansion

In order to write files to a USB volume I want to rename filenames containing '?', '"', '*' or ':' to replace any of these characters with a space. But I'm having trouble with the '*'. My bash script ...
1
vote
2answers
101 views

How to match a file name by specifying a prefix substring?

I would like to copy the files, located on a remote machine, whose filenames match a prefix string, on my local machine. I have tried to select only the files whose names start with either massif or ...
0
votes
1answer
111 views

How to use parameter expansion with find -exec {}? [duplicate]

I want to incorporate something like: for f in */*; do mv "$f" "${f%/*}/foo.${f##*.}"; done Into my find x -exec y {} \;-style workflow. The for loop construct & "$f" variable will ...
2
votes
2answers
61 views

How to access further members of an array when using bash variable indirection?

Consider the following example, it seems it's working fine with the index 0: $ a1=(1 2 3) $ a2=(a b c) $ for x in a1 a2; do echo "${!x}"; done 1 a $ for x in a1 a2; do echo "${!x[0]}"; done 1 a ...
0
votes
0answers
33 views

why bash prompt won't update unless I call bash again [duplicate]

I have this function that echoes the recent directory funtion prompt_oldpwd() { local oldpwd=""; # check if the $OLDPWD is set and not empty if [ ! -z "${OLDPWD}" ]; then # ...
-1
votes
1answer
18 views

Pass variable to execution directive without executing the variable content

I've created a helper function to display a message into the current pane: function current_pane_id { echo "$(tmux display -p '#{pane_id}')" } function display_message { message="$1" "$(tmux ...
8
votes
6answers
2k views

How do I create directories named after each month?

I would like to create a directory name for each month. I know, after some playing with the shell, that: date -d 1/01 +%b # Gives Jan date -d 2/01 +%b # Gives Feb . date -d 12/01 +%b # Gives Dec So ...
0
votes
1answer
39 views

Where is '/?' parameter expansion documented?

I came across this weird parameter expansion for bash/dash which I don't find any reference for. What is this sort of expansion called? Where is it documented? $ /??? bash: /bin: Is a directory $ /???...
1
vote
3answers
58 views

Bash compound substring expansion [duplicate]

I'm aware I can use awk to parse on multiple delims, but that spawns subprocesses. I wanted to know if compound/nested bash parameter expansion is possible. I have PDFs in a directory named like "...
0
votes
1answer
35 views

-q inside Makefile assignment

Been checking a Makefile which contains: ifdef REALLY_QUIET export REALLY_QUIET ECHO := true LFLAGS := $(LFLAGS) -q YFLAGS := $(YFLAGS) -q endif Could you please point me at what's the ...
0
votes
1answer
458 views

ls discrepancy - found vs not found (No such file or directory) - on the same file [closed]

Wildcarding one part of the file name, ls says it exists: $ ls *rammar* London_Grammar_-_Hey_Now_Official_Video-nMEHJPuggHQ.mp4 London_Grammar_-_Hey_Now_Radio_1_s_Big_Weekend_2014-on1QEcz1sTI....
0
votes
2answers
54 views

Expand a variable whose value contains an option and a pathname with whitespaces

$ md="-l /tmp/test/my dir" $ ls "$md" ls: invalid option -- ' ' Try 'ls --help' for more information. $ md="-l \"/tmp/test/my dir\"" $ ls "$md" ls: invalid option -- ' ' Try 'ls --help' for more ...
0
votes
0answers
28 views

Prepend or append to substrings while expanding associative array in GNU Bash

This question was partly inspired by http://mywiki.wooledge.org/BashFAQ/073 , although that page is currently about indexed arrays rather than associative arrays. GNU Bash provides a syntax that ...
0
votes
0answers
167 views

Pass range {$XX..$$YY} to cp command in bash script [duplicate]

I am trying to write a script which will copy a range of files. The range values are picked up from two different files using grep,awk, and cut commands. The file names that I want to copy are in the ...
0
votes
1answer
64 views

Does expansion happen on redirection part of a simple command?

From Bash manual 3.7.1 Simple Command Expansion When a simple command is executed, the shell performs the following expansions, assignments, and redirections, from left to right. The ...
0
votes
0answers
45 views

How to force bash star * wildcard expanding in ascending order for numerical values? [duplicate]

Please assume that we are now within a directory containing 13 files that were named as follows: p1 p2 p3 ... p11 p12 p13 Through bash, when we execute the following command: for i in p*; do echo $...
0
votes
2answers
56 views

Bash script loop with zero paddings for the numbers

So I need to repeatedly try submitting a 4 digit number to a port on the local host, and then evaluate the response I get from the port if it contains a specific string, which I do with grep. I used ...
12
votes
1answer
844 views

bash extended glob - match files without prefix

I'm trying to match filenames for a fail2ban jail - so I need to use filename globbing only - not regexes or bash scripting. My /var/log/apache2 directory contains files including: example.com.error....
9
votes
3answers
1k views

shell expansion (A|B) in filenames?

Is it possible to expand an or choice in the shell when reading a file for example. What I mean by this is that, for instance, grep supports syntax like (A|B) to match A or B in a file. Similarly, ...
0
votes
1answer
49 views

script parameter containing spaces invoking mediainfo

I'd like to create a small shell script that will tell me the duration of a video whose file name is passed to it as a parameter. It'd be something like this: $ script_name "file name with spaces....
0
votes
3answers
486 views

Evaluate captured expression inside replacement in sed

I'm working on deployment script and my need is to replace database (or another system) credentials in configuration file. I have PHP files with credentials like: <?php return [ 'mysql-rw' =&...
12
votes
5answers
959 views

How to make a special expandable phrase in bash?

I find myself doing <command> --help | grep <feature> very very often everyday. I was wondering if it was possible to make something like ^^ that expands to "--help | grep" and then I do ...
1
vote
3answers
432 views

Use variable inside bash brace expansion [duplicate]

I have a program that generates a variable number of pictures, ordered like that: 1.jpg, 2.jpg [...] 10.jpg..etc I can't use *.jpg as input because the 10 will be processed before 2, 3, etc. so I ...
6
votes
3answers
328 views

Is there a parameter substitution/expansion alternative for “| cut -f1,2,3 -d:” a.k.a. trim after and including n-th character occurence?

An ancient version of ipconfig (inside initramfs) requires its user input to supply only up to 7 colon separated elements, like: ip=client-ip:server-ip:gw-ip:netmask:hostname:device:autoconf result ...
7
votes
1answer
569 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 ...
4
votes
1answer
435 views

Whats the difference between $_ and !$? [duplicate]

I've always used !$ to refer to the last argument of the previous command. e.g. ~/dir » mkdir birthday ~/dir » cd !$ ~/dir » cd birthday However I've started seeing tutorials use $_ in place of !$ ...
1
vote
2answers
3k views

how to pass multiple commands to sqlite3 in a one liner shell command

I'm trying to save clipboard content into an sqlite database. Created database and tables. I don't want it to create journal file in every clipboard change, so I tried to pass PRAGMA journal_mode = ...
2
votes
1answer
77 views

Why is the IFS value removed from the file while splitting?

IFS=$'?\n' for line in $(cat "./newfiletoread") do printf "${line}\n" done The file contents are as below: Hello there!How are you doing?How is life?Mine is as boring as a winter morning! The code ...
3
votes
3answers
2k views

bash scripting convert uppercase to lower case and viceversa

Kindly check and advise my script. I'm trying to get a word (min 2, max 5 chars) as its input. User will provide the character position and then the script will change it either from lower to ...
10
votes
4answers
25k views

Escape a variable for use as content of another script

This question is not about how to write a properly escaped string literal. I couldn't find any related question that isn't about how to escape variables for direct consumption within a script or by ...
1
vote
1answer
260 views

How can bash aliases be configured to handle spaces in directory names? [duplicate]

The alias below works well in zsh when spaces in directory names are \ escaped, but bash doesn't handle them properly. What changes would be required to make them work with bash properly. dum() { ...
3
votes
1answer
765 views

error: Macro % has illegal name (%define)

I have a build support script for building my packages. Now I want to automatically set the Version in my spec files. I use the macro %{auto_version} for that. In my script I want to invoke rpmbuild -...
10
votes
2answers
2k views

Is there a maximum to bash file name expansion (globbing) and if so, what is it?

Is there a maximum to bash file name expansion (globbing) and if so, what is it? See globbing on tldp.org. Let's say I want to run a command against a subset of files: grep -e bar foo* rm -f bar* ...
1
vote
1answer
73 views

Difference between word-splitting before expansions and word-splitting during expansions in bash?

Bash manual says that Expansion is performed on the command line after it has been split into words.. However, in the next sentence it states that there are seven kinds of expansion performed and ...
1
vote
3answers
1k views

How to expand array content from its name? [duplicate]

I have an array declare -a arr0=("'1 2 3'" "'4 5 6'") and a variable x=0 Then I create the new variable with the array's name tmp="arr$x" and I'd like to be able to expand arr0 content from ...
0
votes
0answers
24 views

Bash pattern prefix removal script vs. interactive [duplicate]

I used to test my Bash commands interactively on the CLI and then copy them over onto a SH script. This worked fine until today. Run the following on your command line a=008; echo ${a##*(0)} and ...
1
vote
1answer
179 views

Does a sourced bash script contain wrong values for $BASH_SOURCE?

The Linux Documentation Project says: When a file is sourced (by typing either source filename or . filename at the command line), the lines of code in the file are executed as if they were printed ...
0
votes
2answers
9k views

mkdir using variables [duplicate]

I am having issues creating dirs using a variable in a script. Ex: read user mkdir $user{01..10} Interestingly, it will work like this: read user mkdir {01..10}$user Any way to get around this, or ...