Linked Questions
31 questions linked to/from Security Implications of using unsanitized data in Shell Arithmetic evaluation
282
votes
3
answers
57k
views
Security implications of forgetting to quote a variable in bash/POSIX shells
If you've been following unix.stackexchange.com for a while, you
should hopefully know by now that leaving a variable
unquoted in list context (as in echo $var) in Bourne/POSIX
shells (zsh being the ...
32
votes
4
answers
30k
views
Find only the first few matched files using find
Say there are hundreds of *.txt files in a directory. I only want to find the first three *.txt files and then exit the searching process.
How can I achieve this using the find utility? I had a quick ...
8
votes
9
answers
75k
views
Print numbers from 1-50
x=1
while [ $x -le 50 ]
do
echo $x
$x=(($x + 1))
done
I have wrote the above code. What seems to be a easy task in many programming languages is giving this error for me.
solution.sh: line 5: ...
16
votes
2
answers
4k
views
Are there security consequences from not giving printf a format to use?
A well-formed printf usually has a format to use:
$ var="Hello"
$ printf '%s\n' "$var"
Hello
However, what could be the security implications of not providing a format?
$ printf &...
10
votes
3
answers
3k
views
How to reverse shell arguments?
I know that it is possible to reverse "$@" using an array:
arr=( "$@" )
And using this answer, reverse the array.
But that requires a shell that has arrays.
It is also possible using tac:
set -- $(...
7
votes
2
answers
2k
views
This 'while' loop countdown with sleep doesn't work
I'm having some issues with a bash script, but I don't know why. The script is meant to convert the input (in minutes) to seconds and then start counting down until it reaches zero, at which point the ...
5
votes
4
answers
44k
views
How to use and/or conditional in shell script
I have an if statement in a script.
It looks like this:
if [ "$a" != "0" -a "$b" != "100" ]; then
#some commands here
If I'm not mistaken, the line above will work if both conditions are true.
...
19
votes
1
answer
3k
views
How to use associative arrays safely inside arithmetic expressions?
A few Bourne-like shells support associative arrays: ksh93 (since 1993), zsh (since 1998), bash (since 2009), though with some differences in behaviour between the 3.
A common use is for counting ...
8
votes
2
answers
13k
views
String comparison with integer in [[ test
I was looking at discussion between Kusalananda and xhienne here, where it's mentioned [ "" -ge 2 ] not being a valid test producing an error in bash --posix and other POSIX-compliant shells.
bash-4....
12
votes
4
answers
17k
views
Difference between quoting variables in shell script "if" statements?
What is the difference between these two Bash if-statements?
e.g.
if [ "$FOO" = "true" ]; then
vs
if [ $FOO = "true" ]; then
What is the difference? It seems that both statements work the same.
5
votes
3
answers
1k
views
What one should check when re writing bash conditions for sh or ash?
Sometime, it is not possible to have the luxury of bash on a system, but conditions are easier to make on bash compared to sh or ash, what one should verify to ensure condition won't break with ...
5
votes
1
answer
23k
views
conditional binary operator expected
var="$(command1 -l '$var2' -c 'command2|grep -c "search"')"
if [[ var !=0 ]]; then
fi
Why am I getting "conditional binary operator expected". I searched already and. I. see that [[]] is a test ...
4
votes
1
answer
1k
views
What exactly happens when I type " unset * " in prompt?
Say I connected to a Linux system as root.
What exactly happens when I type unset *?
-1
votes
2
answers
375
views
what is the meaning of this shell script function
Can some one tell me what is the meaning of each line with an example ,
I am not getting why regex is used and even [!0122...]
#!/bin/sh
is_integer ()
{
case "${1#[+-]}" in
(*[!...
2
votes
2
answers
1k
views
Bash comparison: protect against syntax error/code injection if the variable contains whitespace and shell syntax
When comparing a variable value to something (I'll take arithmetic comparison so that you can't use the "x$VAR" == "xyes" trick), how do I protect against the case when the ...