All Questions
14 questions
4
votes
2
answers
220
views
How to make the (N) patterns of the zsh eatable by bash?
I am trying to develop a script which runs (and, ideally, does the same :-) ) in zsh and Bash. Problem is, that at a point, the zsh-specific part contains a pattern ending with (N). So: this_pattern*(...
2
votes
1
answer
840
views
Does bash (or zsh) "officially" mandate the use of tabs for indentation in scripts?
I recently came across the following statement (source; emphasis added):
For shell scripts, using tabs is not a matter of preference or style; it's how the language is defined.
I am trying to make ...
0
votes
1
answer
53
views
In a bash/zsh function how do I do something, then depending on the output, do A or B?
This function looks through each local git repository in folder ~/src, and does git pull on it. More and more I keep getting an error
Please commit your changes or stash them before you merge.
...
1
vote
3
answers
2k
views
How do I grep from one character or pattern to another?
I want to grep a string from a given character or pattern until another given character or pattern instead of the entire line.
For example:
$ > echo "The brown fox jumps over the lazy dog" | grep ...
0
votes
4
answers
6k
views
How do I search for a list of strings with find and grep
I am trying to make the following into a bash script:
#!/bin/bash
find /path/to/file -type f -name "*.html" -exec grep -l "XXXX" '{}' \; -print
where XXXX is a list of strings over which this ...
0
votes
2
answers
225
views
How to define the name of a bash function to be a regex
I've searched around and can't find an answer.
I want to define a function like this
function \d{2} () {
echo $1_or_smth
}
Then if I say in my terminal
12
I want it to echo 12 back.
10
votes
4
answers
2k
views
Inverting an associative array
Let's say I have an associative array in bash,
declare -A hash
hash=(
["foo"]=aa
["bar"]=bb
["baz"]=aa
["quux"]=bb
["wibble"]=cc
["wobble"]=aa
)
where both keys and values are ...
1
vote
1
answer
114
views
How to write a function that reliably exits (with a specified status) the current process?
The script below is a minimal (albeit artificial) illustration of the problem.
## demo.sh
exitfn () {
printf -- 'exitfn PID: %d\n' "$$" >&2
exit 1
}
printf -- 'script PID: %d\n' "$$" ...
3
votes
1
answer
638
views
Are there any conventions for the name of global variable that holds function's result?
A typical way for a shell function to "return" its result is to assign it to some global variable.
Is there any convention/best-practice on the name of this variable?
0
votes
1
answer
108
views
Count amount of files/directories in SELECTED directory
I'm trying to make a script that counts files/directories in a specific directory but I have a problem. Here's the script that works for a current directory (count.sh):
ARG3=${1:-d} #default value - ...
153
votes
3
answers
36k
views
Have backticks (i.e. `cmd`) in *sh shells been deprecated?
I've seen this comment many times on Unix & Linux as well as on other sites that use the phrasing "backticks have been deprecated", with respect to shells such as Bash & Zsh.
Is this ...
4
votes
2
answers
288
views
bash equivalent of zsh's $@[2,$#]
(NB: the motivation for this question is only to improve my bash programming know-how.)
I want to know the bash equivalent of zsh expressions such as $@[2,$#], which directly address a range of a ...
11
votes
4
answers
22k
views
Hybrid code in shell scripts. Sharing variables
This answer discusses how to run a multi-line Python snippet from the command line in a terminal. I noticed that the answer works great within shell scripts, even with nested indentation, which is ...
4
votes
4
answers
2k
views
Converting a history to a script?
Often times I've typed in a bunch of commands when I realize that I am going to keep typing them in and that I would really like a script.
Now I understand that no matter how I save my history, I am ...