Questions tagged [syntax]

187 questions
788
votes
5 answers

What does "--" (double-dash) mean?

I have seen -- used in the compgen command. For example: compgen -W "foo bar baz" -- b What is the meaning of the -- in there?
dogbane
  • 29,087
  • 16
  • 80
  • 60
79
votes
3 answers

Is $() a subshell?

I understand the subshell syntax to be (), is $() just a subshell that you can retrieve variable values from? Note: This applies to bash 4.4 based on different wording in their documentation.
leeand00
  • 4,443
  • 10
  • 51
  • 78
38
votes
6 answers

Is ~/Mary a relative path?

Let's suppose Mary is a directory. Is the following path ~/Mary relative?
Alex
  • 449
  • 2
  • 5
  • 4
35
votes
3 answers

What is the meaning of [[:space:]] in bash?

I just came across a bash script. What does [[:space:]] mean in a bash script? Why the double colon?
geraldin
  • 367
  • 1
  • 3
  • 3
31
votes
4 answers

Are if else statement equivalent to logical and && or || and where should I prefer one over the other?

I'm learning about decision making structures and I came across these codes: if [ -f ./myfile ] then cat ./myfile else cat /home/user/myfile fi [ -f ./myfile ] && cat ./myfile || cat /home/user/myfile Both of them behave the same. Are…
Subhaa Chandar
  • 343
  • 3
  • 4
23
votes
4 answers

Preferred syntax for two lines long pipe

When writing a long pipe it is usually clearer to separate it in two lines. This long command line: ruby -run -e httpd -- -p 5000 . 2>&1 | tee >(grep -Fq 'WEBrick::HTTPServer#start' && open localhost:5000) Could be divided as: ruby -run -e httpd…
user232326
21
votes
2 answers

How to pass parameters to function in a bash script?

I'd like to write a function that I can call from a script with many different variables. For some reasons I'm having a lot of trouble doing this. Examples I've read always just use a global variable but that wouldn't make my code much more readable…
user181822
  • 213
  • 1
  • 2
  • 5
20
votes
1 answer

Using mv command to move a file from one folder to another one - Strange example

I'm learning unix commands from a UNIX Tutorial for Beginners at surrey.ac.uk where I found a weird way to move a file. In particular, I have the folder unixstuff in the home directory, inside the unixstuff there is the folder backups and the files…
Gennaro Arguzzi
  • 311
  • 1
  • 7
20
votes
1 answer

Bash script function names containing double colon '::'

I came across a Bash script today that has function names with double colons :: in them, e.g., file::write() and file::read(). I've never seen this syntax before in a Bash script, and when I invoked the script it ran just fine (to my…
Jim Fischer
  • 628
  • 1
  • 5
  • 16
14
votes
2 answers

Why does a brace command group need spaces after the opening brace in POSIX Shell Grammar?

TL;DR: Why does POSIX brace group need spaces after { reserved word but subshell doesn't after reserved word (? POSIX shell grammar defines brace group and subshell as follows brace_group : Lbrace compound_list Rbrace subshell : '('…
Sergiy Kolodyazhnyy
  • 16,187
  • 11
  • 53
  • 104
14
votes
2 answers

How to export a large list of variables in Bash?

I use Bash 4.3.48(1) and I have a .sh file containing about 20 variables right under the shebang. The file contains only variables. This is the pattern: x="1" y="2" ... I need to export all these variables in a DRY way: For example, one export to…
Arcticooling
  • 1
  • 12
  • 44
  • 103
13
votes
4 answers

Empty variable is treated as logical "true" with `$var && action` (shell parsing)

Consider a simple debugging style where $debug would be set either to true or false according to some command-line flag: $debug && echo "Something strange just happened" >&2 I know there are better ways to protect against value setting, such as…
roaima
  • 107,089
  • 14
  • 139
  • 261
12
votes
2 answers

What does command eval "$(/opt/homebrew/bin/brew shellenv)" actually do?

Homebrew requires us to add eval $(/opt/homebrew/bin/brew shellenv) to ~/.zprofile. What does this actually evaluate to and what does this accomplish? I am a bit new to shell scripting. I know $var is used to refer to a variable named var but that's…
Osbridge
  • 123
  • 1
  • 4
12
votes
6 answers

Too many shebang (script declaration) lines --- any way to reduce their amount?

I have a project comprised of about 20 small .sh files. I name these "small" because generally, no file has more than 20 lines of code. I took a modular approach because thus I'm loyal to the Unix philosophy and it's easier for me to maintain the…
Arcticooling
  • 1
  • 12
  • 44
  • 103
10
votes
1 answer

Why can't you reverse the order of the input redirection operator for while loops?

In Bash you can move input redirection operators to the front of a command: cat <<< "hello" # equivalent to <<< "hello" cat Why are you unable to do the same for while loops? while read -r line; do echo "$line"; done <<< "hello" # hello <<<…
philraj
  • 411
  • 1
  • 3
  • 11
1
2 3
12 13