Linked Questions
30 questions linked to/from Escape a variable for use as content of another script
213
votes
6
answers
339k
views
How can we run a command stored in a variable?
$ ls -l /tmp/test/my\ dir/
total 0
I was wondering why the following ways to run the above command fail or succeed?
$ abc='ls -l "/tmp/test/my dir"'
$ $abc
ls: cannot access '"/tmp/test/my': No such ...
19
votes
2
answers
110k
views
How to use a special character as a normal one in Unix shells?
Many questions like 'How to type the double-quote char (")?' are being asked, and we don't want to clutter our community with the same answer (Type it as \" if not enclosed in 's, " if enclosed in 's.)...
29
votes
2
answers
7k
views
How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?
ssh has an annoying feature in that when you run:
ssh user@host cmd and "here's" "one arg"
Instead of running that cmd with its arguments on host, it concatenates that cmd and ...
6
votes
5
answers
2k
views
Download a file with SSH/SCP, tar it inline and pipe it to openssl
I want to download a list of files from a SSH server, then put it in some kind of container (like a tar file) and finally encrypt it (e.g. with openssl).
The point of putting it into a archive is to ...
7
votes
5
answers
2k
views
Decomposition of path specs into longest-common-prefix + suffix
Given any two absolute Unix path specs1, one could decompose each spec as the concatenation of a longest common prefix and a specific suffix. E.g.,
/abc/bcd/cdf -> /abc/bcd + cdf
/abc/bcd/chi/...
7
votes
3
answers
700
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 ...
1
vote
5
answers
11k
views
Remove the matching pattern from grep's output?
With bash, I'm running this: declare -p | grep 'declare -- ' That prints whole lines. I want to print those same lines, but I wanted to exclude the match itself. i.e., Can I do ... | grep pattern | ...
1
vote
5
answers
5k
views
How to add double quotes to every value in env file
I have an .env file and need to copy to an .env.sh file.
The command exports to .env.sh file:
scriptPath=$(dirname "$(readlink -f "$0")")
printenv | sed 's/^\(.*\)$/export \1/g' &...
4
votes
4
answers
7k
views
Passing a command with arguments and redirection to a function
In a Bash script, I pass a command with arguments. This works fine, except when the command includes a redirection. In that case, the redirection character is treated as an ordinary character.
$ cat ...
0
votes
2
answers
2k
views
How to create a text file with path for a script and with directory names?
I have a directory named data. Inside data there is one directory samples which has more than 50 directories and there is one shell script testing.sh inside data. The setup looks like below:
data
|...
5
votes
1
answer
474
views
printf in Zsh does not shell escape exclamation mark
In Zsh 5.9, we can use printf to shell escape a string:
$ printf '%q' 'One! Two'
One\!\ Two
This produces the correct output of escaping the ! and the space. Now let’s make it as a script:
#!/bin/...
4
votes
2
answers
548
views
Use Find to list files and include counter value in output
For performance reasons, I want to use find to list a large number of files, but also include a counter on each line. The following is what I have so far:
local root_dir="."
local ...
5
votes
2
answers
720
views
Is the output of `declare -p <variable>` in bash guaranteed to be reusable as shell input?
This is specifically about bash's declare - the general case is pretty exhaustively dealt with in this answer (which mentions "the typeset/declare/export -p output of ksh93, mksh, zsh" but ...
1
vote
5
answers
318
views
How can I find all of the directories that contain one or more files?
I have a hierarchy of directories. Some directories do not contain files and they only contain other directories. Some contain files.
For example:
- movies
- 2022
- action
- movie.mp4
...
0
votes
2
answers
807
views
Read command removes double space and keeps only one
I wrote a script that collects application logs on local machine and then from remote machine.
It has variable oldlogsdate which reads the date of logs I want to collect.
For example, if I enter Apr ...