Questions tagged [bash-expansion]

71 questions
58
votes
6 answers

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 other programs. My goal is to enable a script to…
Walf
  • 1,254
  • 1
  • 14
  • 19
20
votes
3 answers

How to exclude some files from filename expansion mechanism in bash?

I have ubuntu file system directories in the root directory and I accidentally copied hundreds of files into root directory. I intuitively tried to remove copied files by excluding file system like rm -rf…
kenn
  • 733
  • 2
  • 11
  • 22
17
votes
2 answers

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* Is there a limit to how many files bash will expand…
Thomas BDX
  • 577
  • 5
  • 14
13
votes
1 answer

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…
fireartist
  • 133
  • 5
12
votes
5 answers

How to make a special expandable phrase in bash?

I find myself doing --help | grep very very often everyday. I was wondering if it was possible to make something like ^^ that expands to "--help | grep" and then I do this: ls ^^ size That would execute the following: ls --help…
yukashima huksay
  • 1,154
  • 4
  • 13
  • 29
12
votes
4 answers

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 = OFF; flag. But it's tricky to pass those commands…
kenn
  • 733
  • 2
  • 11
  • 22
11
votes
3 answers

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 mean is how and why the [[ ]] command is…
Guest
  • 119
  • 4
9
votes
3 answers

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, if I have these…
Joe Healey
  • 664
  • 1
  • 6
  • 18
8
votes
5 answers

Why {1,2} printed by a command in $() is not interpolated?

I am in a directory in which I have two text files: $ touch test1.txt $ touch test2.txt When I try to list the files (with Bash) using some pattern it works: $ ls test?.txt test1.txt test2.txt $ ls test{1,2}.txt test1.txt test2.txt However, when…
8
votes
6 answers

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 I have used brace expansion, echo {1..12}/01 and…
JammingThebBits
  • 426
  • 4
  • 13
7
votes
1 answer

File globbing pattern, !(*example), behaves differently in bash script than it does in bash shell

The following works when pasted directly into my bash terminal (I call bash explicitly, bash version: 4.4.19(1)-release (x86_64-pc-linux-gnu)) for filename in /home/dean/Downloads/!(*example).txt; do echo "${filename}" done This command echoes…
dnk8n
  • 233
  • 1
  • 6
7
votes
3 answers

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 in an ipconfig error when users do supply more…
Pro Backup
  • 4,686
  • 12
  • 50
  • 82
7
votes
1 answer

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 identifies the the words "a " (three spaces) and "b",…
antonio
  • 1,381
  • 3
  • 16
  • 37
7
votes
2 answers

Any way to show each step during the command processing?

set -x just show a final result of the command. Any way to view each step more clear, like pics below.
6
votes
2 answers

Print one file per line using echo

How can I print a list of files/directories one-per-line using echo? I can replace spaces with newlines, but this doesn't work if the filenames contain spaces: $ echo small*jpg small1.jpg small2.jpg small photo 1.jpg small photo 2.jpg $ echo…
EmmaV
  • 3,985
  • 4
  • 30
  • 61
1
2 3 4 5