Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
5
votes
3
answers
3894
views
How do I join an array of strings where each string has spaces?
My bash script: #!bin/bash MY_ARRAY=("Some string" "Another string") function join { local IFS="$1"; shift; echo -e "$*"; } join "," ${MY_ARRAY[@]} I want the output to be: `Some string,Another string`. Instead, I get `Some,string,Another,string`. What must I change to get the result I want?
My bash script:
#!bin/bash
MY_ARRAY=("Some string" "Another string")
function join { local IFS="$1"; shift; echo -e "$*"; }
join "," ${MY_ARRAY[@]}
I want the output to be:
Some string,Another string
.
Instead, I get Some,string,Another,string
.
What must I change to get the result I want?
Username
(899 rep)
Aug 30, 2017, 10:22 PM
• Last activity: Jul 17, 2025, 06:30 AM
2
votes
2
answers
135
views
How does `*\ *` work in bash?
There is [an answer from SuperUser][1], which renames filenames containing whitespace: for f in *\ *; do mv "$f" "${f// /_}"; done The part I don't understand is `*\ *`. The author wrote that: > `*\ *` selects all files with a space in their name as input for the the for loop. The pattern `*X*` sele...
There is an answer from SuperUser , which renames filenames containing whitespace:
for f in *\ *; do mv "$f" "${f// /_}"; done
The part I don't understand is
*\ *
.
The author wrote that:
> *\ *
selects all files with a space in their name as input for the the for loop. The pattern *X*
selects all files with X
in their name, and for the special character space, we have to escape it with a slash so that bash doesn't treat it as separating different arguments.
Since *
does not match a space, why does *\ *
also match files with multiple space character when it only has one space in it?
glacier
(391 rep)
Apr 22, 2025, 01:46 PM
• Last activity: May 19, 2025, 04:45 PM
4
votes
1
answers
101
views
How to split output of a shell command on spaces while taking into account quoting?
I have a command that produces a list of arguments, quoting then if neccesary (https://docs.python.org/3/library/shlex.html#shlex.quote). I need them to pass them as command arguments in a `zsh` script (or equivalently, to parse them into an array), which means to split the output on spaces while ta...
I have a command that produces a list of arguments, quoting then if neccesary (https://docs.python.org/3/library/shlex.html#shlex.quote) . I need them to pass them as command arguments in a
zsh
script (or equivalently, to parse them into an array), which means to split the output on spaces while taking the quoting into account.
For example, if the output of my command is foo " bar " baz
, I want to parse this into a zsh
array equivalent to ('foo' ' bar ' 'baz')
.
Petr
(1776 rep)
Apr 8, 2025, 06:34 PM
• Last activity: Apr 8, 2025, 07:27 PM
0
votes
2
answers
2080
views
Exclude the output from ssh and only log the error if found
typeset -f | sshpass -e ssh -o StrictHostKeyChecking=no user@${IPADDRESS} " $(cat); IFERROR=$(checkscript); echo "$IFERROR"" > ${SSHTEMPFILE} 2>&1 This line...I can't exclude the "user authorized" message from the ssh...IFERROR returns the values I need to track, but also, the "!!! AUTHORIZED USE ON...
typeset -f | sshpass -e ssh -o StrictHostKeyChecking=no user@${IPADDRESS} "
$(cat);
IFERROR=$(checkscript);
echo "$IFERROR"" > ${SSHTEMPFILE} 2>&1
This line...I can't exclude the "user authorized" message from the ssh...IFERROR returns the values I need to track, but also, the "!!! AUTHORIZED USE ONLY !!!" horrible message from the ssh...
Already tried something like this, but its not working:
typeset -f | sshpass -e ssh -o StrictHostKeyChecking=no user@${IPADDRESS} "
$(cat);
IFERROR=$(checkscript);
echo "$IFERROR"" | grep -v "AUTHORIZED" > ${SSHTEMPFILE} 2>&1
Vianymoon
(1 rep)
Apr 21, 2015, 07:24 PM
• Last activity: Mar 28, 2025, 09:01 PM
0
votes
1
answers
43
views
tmux quoting problem within display-menu
This command works as expected if I run it in the `tmux` command line: ``` list-panes -a -F "pane ID: #{p3:pane_id} TTY: #{p11:pane_tty}" ``` Output: ``` pane ID: %1 TTY: /dev/pts/35 pane ID: %2 TTY: /dev/pts/36 pane ID: %3 TTY: /dev/pts/37 pane ID: %4 TTY: /dev/pts/38 ``` But I fail in getting them...
This command works as expected if I run it in the
tmux
command line:
list-panes -a -F "pane ID: #{p3:pane_id} TTY: #{p11:pane_tty}"
Output:
pane ID: %1 TTY: /dev/pts/35
pane ID: %2 TTY: /dev/pts/36
pane ID: %3 TTY: /dev/pts/37
pane ID: %4 TTY: /dev/pts/38
But I fail in getting them to work within display-menu
. The problem seems obvious (at least from my shell perspective): An additional level of quoting is required so that list-panes
sees the format string and not its resolved value. But obviously I am doing it wrong:
bind-key C-t display-menu -T 'Test' \
'List panes with tty' '' "list-panes -a -F 'pane ID: \#\{p3:pane_id\} TTY: \#{p11:pane_tty}"
bind-key C-t display-menu -T 'Test' \
'List panes with tty' '' {
list-panes -a -F 'pane ID: #{p3:pane_id} TTY: #{p11:pane_tty}'
}
Output:
pane ID: %1 TTY: /dev/pts/35
pane ID: %1 TTY: /dev/pts/35
pane ID: %1 TTY: /dev/pts/35
pane ID: %1 TTY: /dev/pts/35
Hauke Laging
(93678 rep)
Mar 9, 2025, 12:04 AM
• Last activity: Mar 9, 2025, 10:40 AM
0
votes
1
answers
74
views
How does `find` replace `{}` if it contains special characters like `"`?
Recently when I read [one QA][1], it has some unexpected behaviors for me: ```bash ~/findtest % echo three > file\ with\ \"double\ quotes\" ~ % find findtest -type f -exec sh -c 'set -x;cat "{}"' \; + cat 'findtest/file with double' quotes cat: findtest/file with double: No such file or directory ca...
Recently when I read one QA , it has some unexpected behaviors for me:
~/findtest % echo three > file\ with\ \"double\ quotes\"
~ % find findtest -type f -exec sh -c 'set -x;cat "{}"' \;
+ cat 'findtest/file with double' quotes
cat: findtest/file with double: No such file or directory
cat: quotes: No such file or directory
IMHO when doine replacement, it will do 'cat findtest/file\ with\ \"double\ quotes\"'
if just replacing the above filename file\ with\ \"double\ quotes\"
which will work. If we keep the quote wrapper, it will show:
$ cat "findtest/file\ with\ \"double\ quotes\""
cat: 'findtest/file\ with\ "double\ quotes"': No such file or directory
Q:
What does that replacement actually do causing the above a bit weird behavior with 2 substr 'findtest/file with double' and 'quotes' but not 'findtest/file with' and 'double quotes' with "
as the delimeter?
An5Drama
(173 rep)
Mar 7, 2025, 08:32 AM
• Last activity: Mar 7, 2025, 10:38 AM
0
votes
2
answers
87
views
Put quotes around strings between commas, but not numbers
So I have some text in a file that almost represents CSV format, but not entirely. I want to turn in into a CSV file though. I have for example: cuz,0,1,2,3,-4,abc,a b c,0 How can I turn that into "cuz",0,1,2,3,-4,"abc","a b c",0 Any help would be appreciated! Thanks in advance.
So I have some text in a file that almost represents CSV format, but not entirely. I want to turn in into a CSV file though.
I have for example:
cuz,0,1,2,3,-4,abc,a b c,0
How can I turn that into
"cuz",0,1,2,3,-4,"abc","a b c",0
Any help would be appreciated!
Thanks in advance.
user1840352
(13 rep)
Apr 25, 2017, 08:41 PM
• Last activity: Mar 3, 2025, 09:07 AM
36
votes
4
answers
177240
views
What is the difference between echo `date`, echo "`date`", and echo '`date`'?
What is the difference between these three commands? echo `date` echo "`date`" echo '`date`' I am confused on what the differences actually are. I think that when the ' are around it means that it is a string, therefore echo would literally output the string `date` instead of displaying the date?
What is the difference between these three commands?
echo
date
echo "date
"
echo 'date
'
I am confused on what the differences actually are. I think that when the ' are around it means that it is a string, therefore echo would literally output the string date
instead of displaying the date?
John
(3797 rep)
Nov 1, 2013, 05:33 AM
• Last activity: Feb 3, 2025, 06:12 AM
138
votes
6
answers
124109
views
How to run a command that involves redirecting or piping with sudo?
I am trying to follow what I assume is best practices of using sudo instead of root account. I am running a simple concat file operation such as: sudo echo 'clock_hctosys="YES"' >> /etc/conf.d/hwclock This fails as to the right of the ">>" it is running as the normal user. Adding extra sudos also fa...
I am trying to follow what I assume is best practices of using sudo instead of root account.
I am running a simple concat file operation such as:
sudo echo 'clock_hctosys="YES"' >> /etc/conf.d/hwclock
This fails as to the right of the ">>" it is running as the normal user. Adding extra sudos also fails (expected behaviour since piping to the sudo command and not to the file).
Example is just that but it has been verified and tested under the root account.
DarkSheep
(1966 rep)
Dec 26, 2013, 12:41 PM
• Last activity: Jan 25, 2025, 09:37 AM
0
votes
2
answers
610
views
Why do these rsync filter args fail in bash when passed in array?
Why does this rsync command work when I give it literally but not when I create it from variables? Here are the variables - first the options I'm passing to rysnc, as an array: $ echo "${options[@]}" -av --prune-empty-dirs -f "- *.flac" -f "- *.WMA" -f "- *.wma" -f "- *.ogg" -f "- *.mp4" -f "- *.m4a...
Why does this rsync command work when I give it literally but not when I create it from variables?
Here are the variables - first the options I'm passing to rysnc, as an array:
$ echo "${options[@]}"
-av --prune-empty-dirs -f "- *.flac" -f "- *.WMA" -f "- *.wma" -f "- *.ogg" -f "- *.mp4" -f "- *.m4a" -f "- *.webm" -f "- *.wav" -f "- *.ape" -f "- *.zip" -f "- *.rar"
$ echo ${options}
-f
$ echo ${options}
"- *.wma"
Then the source directory, from which rsync is to copy files:
$ echo "\"$dir/\""
"/media/test/Ahmad Jamal Trio/Live at the Pershing/"
And the target directory, to which rsync is to copy files:
$ echo "\"$target_dir\""
"/home/test/mp3/Ahmad Jamal Trio/Live at the Pershing/"
Put it all together:
$ echo "${options[@]}" "\"$dir/\"" "\"$target_dir\""
-av --prune-empty-dirs -f "- *.flac" -f "- *.WMA" -f "- *.wma" -f "- *.ogg" -f "- *.mp4" -f "- *.m4a" -f "- *.webm" -f "- *.wav" -f "- *.ape" -f "- *.zip" -f "- *.rar" "/media/test/Ahmad Jamal Trio/Live at the Pershing//" "/home/test/mp3/Ahmad Jamal Trio/Live at the Pershing/"
That all looks like it should. And indeed, it does work if you give the command literally, like this:
$ rsync -av --prune-empty-dirs -f "- *.flac" -f "- *.WMA" -f "- *.wma" -f "- *.ogg" -f "- *.mp4" -f "- *.m4a" -f "- *.webm" -f "- *.wav" -f "- *.ape" -f "- *.zip" -f "- *.rar" "/media/test/Ahmad Jamal Trio/Live at the Pershing/" "/home/test/mp3/Ahmad Jamal Trio/Live at the Pershing/"
./
Ahmad Jamal Trio - Live at the Pershing - 01 - But Not for Me.mp3
Ahmad Jamal Trio - Live at the Pershing - 02 - Surrey With The Fringe On Top.mp3
Ahmad Jamal Trio - Live at the Pershing - 03 - Moonlight In Vermont.mp3
Ahmad Jamal Trio - Live at the Pershing - 04 - Music, Music, Music.mp3
Ahmad Jamal Trio - Live at the Pershing - 05 - No Greater Love.mp3
Ahmad Jamal Trio - Live at the Pershing - 06 - Poinciana.mp3
Ahmad Jamal Trio - Live at the Pershing - 07 - Wood'yn You.mp3
Ahmad Jamal Trio - Live at the Pershing - 08 - What's New.mp3
AlbumArtSmall.jpg
AlbumArtLarge.jpg
Folder.jpg
sent 43,194,376 bytes received 285 bytes 28,796,440.67 bytes/sec
total size is 43,182,454 speedup is 1.00
But it fails when I call rsync using the vars as args:
$ rsync "${options[@]}" "\"$dir/\"" "\"$target_dir\""
Unknown filter rule: `"- *.flac"'
rsync error: syntax or usage error (code 1) at exclude.c(902) [client=3.1.2]
markling
(231 rep)
Nov 5, 2019, 10:40 AM
• Last activity: Dec 29, 2024, 11:04 AM
21
votes
2
answers
12868
views
How Can I Expand A Tilde ~ As Part Of A Variable?
When I open up a bash prompt and type: $ set -o xtrace $ x='~/someDirectory' + x='~/someDirectory' $ echo $x + echo '~/someDirectory' ~/someDirectory I was hoping that the 5th line above would have went `+ echo /home/myUsername/someDirectory`. Is there a way to do this? In my original Bash script, t...
When I open up a bash prompt and type:
$ set -o xtrace
$ x='~/someDirectory'
+ x='~/someDirectory'
$ echo $x
+ echo '~/someDirectory'
~/someDirectory
I was hoping that the 5th line above would have went
+ echo /home/myUsername/someDirectory
. Is there a way to do this? In my original Bash script, the variable x is actually being populated from data from an input file, via a loop like this:
while IFS= read line
do
params=($line)
echo ${params}
done <"./someInputFile.txt"
Still, I'm getting a similar result, with the echo '~/someDirectory'
instead of echo /home/myUsername/someDirectory
.
Andrew
(312 rep)
Oct 20, 2017, 06:34 PM
• Last activity: Dec 26, 2024, 01:34 AM
6
votes
4
answers
7169
views
shell: Quote string with single quotes rather than backslashes
How can I quote a string with single quotes? Eg, I can do: $ printf "%q\n" 'two words' two\ words $ Is there a way to get a single- (or double-) quoted string as output, ie: $ MAGIC 'two words' 'two words' $ I find the single-quoted version much easier to read. I'd like an answer which works for {ba...
How can I quote a string with single quotes?
Eg, I can do:
$ printf "%q\n" 'two words'
two\ words
$
Is there a way to get a single- (or double-) quoted string as output, ie:
$ MAGIC 'two words'
'two words'
$
I find the single-quoted version much easier to read.
I'd like an answer which works for {ba,z}sh. POSIX shell would be a bonus.
Tom Hale
(32892 rep)
May 23, 2018, 04:14 AM
• Last activity: Dec 15, 2024, 05:10 PM
11
votes
5
answers
20626
views
Properly escaping output from pipe in xargs
Example: % touch -- safe-name -name-with-dash-prefix "name with space" \ 'name-with-double-quote"' "name-with-single-quote'" \ 'name-with-backslash\' `xargs` can't seem to handle double quotes: % ls | xargs ls -l xargs: unmatched double quote; by default quotes are special to xargs unless you use th...
Example:
% touch -- safe-name -name-with-dash-prefix "name with space" \
'name-with-double-quote"' "name-with-single-quote'" \
'name-with-backslash\'
xargs
can't seem to handle double quotes:
% ls | xargs ls -l
xargs: unmatched double quote; by default quotes are special to xargs unless you use the -0 option
ls: invalid option -- 'e'
Try 'ls --help' for more information.
If we use the -0
option, it has trouble with name that has dash prefix:
% ls -- * | xargs -0 -- ls -l --
ls: invalid option -- 'e'
Try 'ls --help' for more information.
This is before using other potentially problematic characters like newline, control character, etc.
Gerry Lufwansa
(523 rep)
Aug 11, 2017, 12:17 PM
• Last activity: Dec 6, 2024, 11:00 AM
1
votes
1
answers
93
views
Quotation marks in filenames. Can they mess with quotation marks in shell code?
As far as I know, a safe and portable filename can consists of aA-zZ 0-9 hyphen and underscore only. At the same time, if we move away from the safe file-naming practices, we can use characters such as `!` `?` `$` `%` `"` `'` `/` and so on. My question is about quotes, that is, `"` and `'`. Isn't it...
As far as I know, a safe and portable filename can consists of aA-zZ 0-9 hyphen and underscore only. At the same time, if we move away from the safe file-naming practices, we can use characters such as
!
?
$
%
"
'
/
and so on.
My question is about quotes, that is, "
and '
. Isn't it they are more dangerous than, for example, $
or /
? For example, if a filename contains spaces and we need to copy such a file, we would enclose its name quotes:
cp "file with spaces" folder_without_spaces
And because of this, I question myself: Isn't that quotes in a filename can mess with quotes in shell code sometimes?
For example, you have
aaa aaa ccc.txt
aaa "xxx" ccc.txt
and then you use
for f in *.txt; do mv "$f" backup_dir; done
and then "Bam!" - something broke. Nothing is broken actually in this particular example (at least on Zsh), but I hope you understood what I mean.
jsx97
(1347 rep)
Nov 6, 2024, 12:19 PM
• Last activity: Nov 14, 2024, 10:39 PM
0
votes
2
answers
521
views
Why does the "cp" command not work with pasted directory path from "pwd | pbcopy" command?
I ran `pwd | pbcopy` command in Terminal on my MacBook Pro to copy the working directory to the clipboard. The working directory looks like this: ``` /Users/JohnSmith/PycharmProjects/100 Days of Code - The Complete Python Pro Bootcamp2/Day 2/Type Error, Checking and Conversion ``` But when I change...
I ran
pwd | pbcopy
command in Terminal on my MacBook Pro to copy the working directory to the clipboard. The working directory looks like this:
/Users/JohnSmith/PycharmProjects/100 Days of Code - The Complete Python Pro Bootcamp2/Day 2/Type Error, Checking and Conversion
But when I change to another completely different directory, type cp
followed by CMD+V to paste the first directory path I get this error message:
cp: Conversion is not a directory
However, when I edit the clipboard content to append a forward slash character "/", I do not get the error message above. But when I then append the name of the file that I want to copy like this;
/Users/StephenLearmonth/PycharmProjects/100 Days of Code - The Complete Python Pro Bootcamp2/Day 2/Type Error, Checking and Conversion/task.py task-2.py
I get this error message:
cp: task-2.py is not a directory
It only copies the file task.py
when I put double quotes around the directories in the copied directory path.
Is this a bug with Unix or the pwd | pbcopy
command?
Anyone else had this problem?
sjl26916091
(35 rep)
Nov 13, 2024, 02:21 PM
• Last activity: Nov 14, 2024, 10:16 PM
4
votes
1
answers
339
views
How to explicitly set the tabname of a new gnome-terminal?
I want to explicity rename a tab in gnome-terminal on startup of the tab. I don't want to use gnome-terminal --title flag as that gets reset by my systems bashrc file after whatever else is supposed to run. I have used this command with success in a normal terminal export PROMPT_COMMAND="echo -ne '\...
I want to explicity rename a tab in gnome-terminal on startup of the tab. I don't want to use gnome-terminal --title flag as that gets reset by my systems bashrc file after whatever else is supposed to run.
I have used this command with success in a normal terminal
export PROMPT_COMMAND="echo -ne '\033]0;TABNAME\007'"
This command works fine to rename the current tab's name, but when I try to use it in conjunction with gnome-terminal execute command, I am not getting proper output.
I have used gnome-terminal --e flag to execute simple commands with success, something like this will bring up a new terminal and echo hey then return to bash
gnome-terminal -e "bash -c 'echo hey';bash"
Here is what I am trying ***note the escaped " " marks that I added***
TABNAME="export PROMPT_COMMAND=\"echo -ne '\033]0;TABNAME\007'\""
gnome-terminal --tab --e "bash -c $TABNAME;bash"
I always get weird output no matter how I change the quotes, but I think that is where the problem is.
Prodnegel
(141 rep)
Jan 31, 2017, 08:35 PM
• Last activity: Nov 14, 2024, 08:27 AM
0
votes
3
answers
148
views
How to escape both single quotes and exclamation marks in bash
I have a long command and I just want to use `alias` to shorten it. But the command contains single quotes and exclamation marks. The origin command is `ldapsearch -x -H ... -w 'abc!123'`. I tried `alias search='ldapsearch -x -H ... -w \'abc!123\''` or `alias search="ldapsearch -x -H ... -w 'abc!123...
I have a long command and I just want to use
alias
to shorten it. But the command contains single quotes and exclamation marks.
The origin command is ldapsearch -x -H ... -w 'abc!123'
.
I tried alias search='ldapsearch -x -H ... -w \'abc!123\''
or alias search="ldapsearch -x -H ... -w 'abc!123'"
and so on. But none of them works for me.
Donghua Liu
(103 rep)
May 31, 2024, 06:27 AM
• Last activity: Nov 9, 2024, 01:34 PM
0
votes
0
answers
21
views
Understanding syntax of output from alias
I don't understand how to read the string and the escaping of the following output from listing aliases ```bash > alias foo="echo 'hello'" > alias ... alias foo='echo '\''hello'\''' ... ``` It doesn't make sense to me because `'\'` should be a string, then `'hello'`, but then there would be `\'` an...
I don't understand how to read the string and the escaping of the following output from listing aliases
> alias foo="echo 'hello'"
> alias
...
alias foo='echo '\''hello'\'''
...
It doesn't make sense to me because '\'
should be a string, then 'hello'
, but then there would be \'
an escaped single quote, and then an unmatched single quote '
Probably '\''
is supposed to be just a string with an escaped '
, but I thought single quotes cannot appear within single quoted strings, it will just be interpreted as the end quote
hl5619
(1 rep)
Nov 7, 2024, 03:01 AM
• Last activity: Nov 7, 2024, 03:29 AM
3
votes
2
answers
6748
views
Interpolate within single quotes
I have the following: COMMENT="A random comment" TEXT_JSON='{"person" : "Jim","comment" : "$COMMENT"}' echo "$TEXT_JSON" | jq . This prints { "person": "Jim", "comment": "$COMMENT" } which is not what I want. How can I interpolate the string here?
I have the following:
COMMENT="A random comment"
TEXT_JSON='{"person" : "Jim","comment" : "$COMMENT"}'
echo "$TEXT_JSON" | jq .
This prints
{
"person": "Jim",
"comment": "$COMMENT"
}
which is not what I want. How can I interpolate the string here?
Jim
(1479 rep)
Jun 5, 2018, 11:16 AM
• Last activity: Oct 22, 2024, 09:01 AM
6
votes
3
answers
954
views
bash script quoting frustration
This problem is driving me crazy. From the command prompt I can enter this command and it works as expected (records where the INFO/RegionType tag contains the value Core are emitted in the output file): ``` bcftools filter -i "INFO/RegionType='Core'" -Oz -o temp.core.vcf.gz temp2.vcf.gz ``` But whe...
This problem is driving me crazy. From the command prompt I can enter this command and it works as expected (records where the INFO/RegionType tag contains the value Core are emitted in the output file):
bcftools filter -i "INFO/RegionType='Core'" -Oz -o temp.core.vcf.gz temp2.vcf.gz
But when I try to put the command in a bash script things go awry. This seems to be due to the single and double quotes? I tried various permutations of escaping quotes with no success. Finally I tried the approach described here:
to store the filter string in a variable and reference the variable. From my script:
filter_string=INFO/RegionType="'Core'"
bcftools filter -i \""$filter_string"\" -Oz -o temp.core.vcf.gz temp2.vcf.gz
This completes without an error but the command is not interpreting the filter correctly as evidenced by the fact that no records are included in the output file, whereas the command given directly from the command prompt yields 3124 records in the output file.
What might be going on here?
----
A minimal example of the input file that works with bcftools
and reproduces the issue (note that all whitespace in the lines starting with chr1
needs to be single tabs, not spaces):
-none
##fileformat=VCFv4.2
##FILTER=
##INFO=
##INFO=
##FORMAT=
##contig=
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT sample1
chr1 103385791 . C G 1182.77 PASS AC=1; GT 0/1
chr1 103471456 . CCAT C 2848.73 PASS AC=2;RegionType=Core GT 1/1
mcrepeau
(69 rep)
Aug 21, 2024, 08:17 PM
• Last activity: Aug 23, 2024, 05:17 AM
Showing page 1 of 20 total questions