All Questions
503
questions
0
votes
1
answer
57
views
value of $VAR already contains backtick and/or single quote inside. How to handle it? How to properly pass $VAR to program? [duplicate]
$ bash --version
GNU bash, versione 5.2.26(1)-release (x86_64-pc-linux-gnu)
I don't know how to deal with $VAR when its value inside contains single quote (') and/or backtick (`).
I'm in the need of ...
-1
votes
1
answer
42
views
I can't grep some inputrc string
bind -p |grep -E "\\e.\":" work
but
bind -p |grep -E "\\e\\C-.\":" don't work
I tried a lot of combination
5
votes
1
answer
450
views
ls output display a file named "N'*" as "N'\''*"
System:
Ubuntu 22.04.3 LTS
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
ls (GNU coreutils) 8.32
Situation:
$ touch "N'*"
$ ls
'N'\''*'
"GNU Coreutils - Quoting File ...
1
vote
1
answer
130
views
bash cd issue with path containing spaces: "too many arguments" [closed]
I created a path with spaces, and when I try to change directory I get "too many arguments" error message despite escaping the spaces or quoting the path :
Here are the tests I made :
# ...
-1
votes
3
answers
94
views
How to use a variable in a command inside of a bash file
I use this command directly on our redhat linux server 8.8 and it's working correctly and I get the result I want:
grep '01-FEB-2024' /u01/app/server1/listener_scan/trace/listener_scan.log | awk '{ if ...
0
votes
0
answers
59
views
Why isn't passed quoted $@ a single argument? [duplicate]
Why isn't passed quoted $@ a single argument?
f2()
{
echo "f2: $1"
}
f1()
{
local x=("$@")
f2 "${x[@]}"
}
f1 x y
Invocation:
$ bash t537.sh
f2: ...
0
votes
0
answers
45
views
escape in double and single quotation [duplicate]
I am confusing why bash can escape "..." but can't escape '...'? Can some one give me a hint? Thanks
test@test:~$ echo "He said, \"Hello world\""
He said, "Hello ...
1
vote
1
answer
100
views
Why does -n with unquoted variable containing empty string return true? [duplicate]
From man bash:
-n string
True if the length of string is non‐zero.
Examples:
# expected
$ var=""; [ -n "$var" ]; echo $?
1
# unexpected?
$ var=""; [ -n $var ]; echo ...
2
votes
1
answer
229
views
How to proberly deal with quotes in filenames when using variables/arrays including the filenames in bash? [duplicate]
I have been working on a bash script for a few days and got stuck with filenames including single and double quotes.
Give I want to iterate over the following files in a directory:
'file1.txt'
'file2....
0
votes
1
answer
60
views
How can I edit matching files if the paths contain whitespace in bash?
I sometimes grep (or rg aka ripgrep or ag aka silversearcher) for things and then want to edit the matching files. For the editor part I use bash's history.
~$ rg someThing
./path/to/some/file.txt
1:...
0
votes
1
answer
132
views
how to write function with nested commands [duplicate]
I'm trying to write a find and cd function like so:
findcd () {
cd "$(dirname "$(find '$1' -type '$2' -name '$3')")"
}
to be called like so:
find . f [FILE_NAME]
But it's ...
1
vote
1
answer
179
views
Run `git commit -m` with single quotes in zsh
I sometimes use characters such as ! and $ in commit messages, they need to be manually escaped, but not if you use single quotes like this git commit -m 'My $message here!'. I tried to write a ...
5
votes
2
answers
601
views
Bash reads quotes inside a variable as text, not quotes? Is "Implicit quoting" a thing in Bash?
I've got a bash script that cleans up the mail queue periodically. For Reasons, we've elected to delete any email to @mms.att.net and other email2SMS gateways that are over 9 hours in the queue and ...
7
votes
2
answers
1k
views
Echoing "!" inside a string does some weird things [duplicate]
If I type in this:
echo "Hello, World!"
I don't know the name of it, but it prompts me for the next line. You know the PS2 thing. Or if you type echo \ and press Enter.
Why?
Well I know ...
0
votes
1
answer
73
views
Using a variable in between ' 's
I have a bash script with a line that was originally this
convert '%d.jpg[1-300]' combined.pdf
Uses convert from Imagemagick to strap a load of sequentially numbered jpgs in to a PDF.
I've written a ...
0
votes
2
answers
190
views
Why is echo "\*" the same as echo "\\*"
Title.
echo "\*" has the exact same output as echo "\\*", \*. I am using GNU bash 5.2.15.
I expected echo "\\*" to output \*, but I do not know why echo "\*" ...
0
votes
2
answers
213
views
Show quotes with echo inside bash -c
I am trying to echo I say "Hello, World!" with bash -c. This is (some of) what I have tried:
$ bash -c "echo I say \"Hello, World"'!'"\""
$ bash -c "echo I ...
7
votes
3
answers
636
views
Is it possible to print a command so that the output can be parsed to create the same command?
The question may sound quite complicated, but in fact it is not! Consider:
% f() { echo "$@"; }
% f a
a
% f cmd -o"value with space"
cmd -ovalue with space
% f cmd -ovalue with ...
3
votes
1
answer
387
views
Problem with single quote in Bash [duplicate]
I have a sh script for initializing dzen2, and I want to use Source Code Pro font to use there. Here's the code...
param="-p -dock -fn 'Source Code Pro-10'"
# the generated_output function ...
3
votes
1
answer
404
views
Why is "${1-"$var"}" (option 6 down below) not mentioned in POSIX?
The only reference I could find in the spec is this:
It would be desirable to include the statement "The characters from an enclosed "${" to the matching '}' shall not be affected by ...
6
votes
1
answer
3k
views
Getting jq error when using variable in command line
I am using jq to get the tag_name index with the script:
curl \
https://api.github.com/repos/checkstyle/checkstyle/releases \
-H "Authorization: token $GITHUB_TOKEN" \
-o /var/tmp/cs-...
2
votes
1
answer
969
views
How to properly use quotes for a bash function?
I am trying to define the following bash function in my .bashrc file:
function myfind() {
find $1 -not -path venv -not -path .tox -name "$2" | xargs grep -n "$3"
}
which is ...
3
votes
1
answer
280
views
Recursively renaming apostrophs ' in filenames (Bash)
Need to remove apostrophes from files. I have tried several approaches, also from Stackexchange.
I am on a Synology NAS, so I don't have Python, or Perl and furthermore I have to exclude certain ...
2
votes
1
answer
2k
views
pass long html string as argument to bash script (Filename too long)
I am passing an html string as an input to a bash script:
Script.py
def bash(commandStr):
subprocess.Popen(commandStr, shell=True, executable="/bin/bash")
def sendmail(subject, to, ...
2
votes
1
answer
59
views
Why isn't the `else` arm executed in this Bash script (for loop through an array)?
The following code is meant to look for subdirectories in ~/Downloads. I run it with . ./script.sh. It will find them even when the user submits an incomplete name.
#!/usr/bin/bash
echo -e "\...
0
votes
2
answers
236
views
How do I convert this script into an alias (MacOS, ZSH) [duplicate]
This script works fine when directly typed into the console:
N | find . -type f -iname "*.aac" -exec bash -c 'FILE="$1"; ffmpeg -i "${FILE}" -acodec libmp3lame "${...
1
vote
2
answers
341
views
Please explain the behavior of these parameter expansions using IFS?
I'm trying to figure out how to use the ${parameter%word} expansion with $@ and $*. It started by trying to make a script to combine pdfs using ghostscript, but I ran into some weird behavior with ...
1
vote
2
answers
4k
views
Bash: How do I quote shell arguments with spaces inside a variable containing multiple arguments without using an array? [duplicate]
In Bash, is it possible to pass a variable with a properly-quoted list of options to a command and not have it split on whitespace inside quotes? IOW, this script:
MYCONFIG="--hi FOO=bar 'X=ABC ...
0
votes
0
answers
25
views
Quoting inside [[ ]] in bash shell [duplicate]
Inside [[ ]] I do not need to quote variables right?
Rather than
if [[ "$flrm" == *"org"* || "$flrm" == "all" ]]; then
printf '%s\n' "Delete: $fl"
...
1
vote
2
answers
1k
views
Array Declaration: Double Quotes & Parentheses
Perhaps this is a stupid question but two hours on Google hasn't turned up anything on point.
Simply, does a difference exist in Bash between:
X="
a
b
c
"
and
X=(
a
b
c
)
The former ...
4
votes
2
answers
948
views
Bash's read builtin errors on a string-based timeout option specification but not an array-based one. Why?
In reading through the source to fff to learn more about Bash programming, I saw a timeout option passed to read as an array here:
read "${read_flags[@]}" -srn 1 && key "$REPLY&...
2
votes
2
answers
1k
views
Removing single quotes from double-quoted variable element in array and run a command [duplicate]
I have a script where I dinamically change the arguments which must be passed to a command (mkvpropedit in this case). Consider the example script below:
#!/bin/bash
LANG_NAME="eng lish"
...
0
votes
1
answer
382
views
case-substring function and quoting
i use a simple function in a script in order to ckeck if a string contains a particular substring:
#!/bin/bash
#
subs() {
case $2 in
*$1*)
return 0 ;;
*)
...
3
votes
2
answers
280
views
Why does the command echo `echo \\\\\\\z` in bash script print \\z instead of \\\z?
The command
echo `echo \\\\\\\z`
is from this book , I don’t understand why it prints
\\z
when it get executed via the bash script.
I think it should print
\\\z
2
votes
1
answer
99
views
Does + need to be escaped in find commands?
I have the following code in bash and I wonder on whether I can get rid of \+ and simply use +
find "$fdir" \
-type f -name "*.org" -o -name ...
5
votes
3
answers
1k
views
How to recursively set directory permissions with a find that lacks -exec?
My Qnap NAS is cursed with a find command that lacks the -exec parameter, so I have to pipe to something. The shell is: GNU bash, version 3.2.57(2)-release-(arm-unknown-linux-gnueabihf)
I'm trying to ...
0
votes
1
answer
729
views
BASH ANSI-C Quoting - what is the Escape character \E
According to the GNU Bash manual (section 3.1.2.4 ANSI-C Quoting) $'\E' should expand to "an escape character (not ANSI C)". What is this escape character? Is it the character equivalent to ...
0
votes
2
answers
476
views
Pass qouted filename with spaces in a bash variable [duplicate]
In a bash shell, consider this:
$ x="-name 'foo bar'"
$ find $x
find: paths must precede expression: `bar''
$ find -name 'foo bar'
./foo bar
What can I put in $x to make find $x behave like ...
3
votes
1
answer
338
views
Why does substituting eval with declare (for creating dynamic variables) result in an empty variable?
With bash >5, I'm trying to assign a different value to variables depending on the architecture specified in a variable. I use a function to do so. This works perfectly:
# arguments:
variable ...
3
votes
1
answer
110
views
Cannot understand this bash shell argument expansion
Totally crazy about the following commands:
declare -a partition_files
readarray -d '' partition_files < <(find "$choosen_image_folder" -name "*sda${i}.gz*")
# this does not ...
5
votes
2
answers
496
views
Why does quoting not protect filenames that start with "-" against mis-interpretation? [duplicate]
This might be a ridiculous post, but let's say we have a file called -|h4k3r|-, why doesn't this quoting work?
cat '-|h4k3r|-'
I thought single quotes remove meaning of all special characters?
...
2
votes
2
answers
696
views
Bash interpreting special character within single quotes [duplicate]
Ran into some unexpected but interesting behavior. I was doing a somewhat complex execution when I ran into a situation that from my understanding of how Bash handles characters shouldn't have ...
7
votes
3
answers
676
views
How would you gracefully handle this snippet to allow for spaces in directories?
I have a series of commands, e.g.:
ssh -i key 10.10.10.10 mkdir -p "${DEST_PATH}/subdir1" "${DEST_PATH}/subdir2"
rsync "${SOURCE_PATH}" "$DEST_HOST:${DEST_PATH}/...
3
votes
1
answer
9k
views
Escaping backlash and double quotes inside a sed expression (surrounded by double quotes)
$ echo 'output: " ' | sed "s/\"/\"/"
output: "
$ echo 'output: " ' | sed "s/\"/\\\"/"
output: "
$ echo 'output: " ' | sed "s/\&...
2
votes
1
answer
1k
views
How to work around wildcard inside double quotes? [duplicate]
In bash, the file globbing * doesn't work inside double quotes, but my filename contains whitespace, so I need to double quote filename before passing it to a shell script. How can I do that?
For ...
-2
votes
3
answers
1k
views
quoting problem with mysql
I'm running the following command and receiving an error related to quoting or something.
#!/bin/bash
test='my_table'
date='2020-10-31 00:00:00.000000'
mysql --user=halloween --password='ghost_123' --...
1
vote
1
answer
353
views
To what does an unquoted empty variable expand in bash?
To what value does an unquoted, undeclared variable expand to in order to return an exit status of 0?
Here is an example of a particular situation I ran into:
[ -n $var ]; echo $?
0
[ -n "$var&...
5
votes
3
answers
5k
views
Escaping double quotes inside command substitution
I am attempting to write a simple bash parser. I am following the steps in this wiki. One of the assumptions I make is that I can do a pass over the entire input string and remove all single and ...
1
vote
1
answer
973
views
Command substitution inside double quotes
I am attempting to write a bash parser. Many resources have referred to this wiki
One area I am getting stuck is why the following would work
echo "$(echo "hi")" # output => ...
1
vote
2
answers
226
views
How to construct bash command and arguments for ssh / su -c
I'm backing stuff up from an array list of sources, a bit like this simplified version:
sources=( file1 file2 "other stuff" )
backupcommand="rsync -ar ${a[@]} dest/"
su backupuser -...