Questions tagged [syntax]

215 questions
852
votes
5 answers

What does "--" (double dash / double hyphen) 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
  • 30,975
87
votes
3 answers

Is $() a subshell?

I understand the subshell syntax to be (<commands...>) , 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
  • 5,042
  • 12
  • 64
  • 88
38
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
  • 397
38
votes
6 answers

Is ~/Mary a relative path?

Let's suppose Mary is a directory. Is the following path ~/Mary relative?
Alex
  • 449
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 there any…
29
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…
Osbridge
  • 393
25
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…
24
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
24
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 surprise).…
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…
15
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 : '(' compound_list…
14
votes
2 answers

Why do shell control statement have that syntax (semicolon/newline+then/do)

I of course realize the need to have something that separates the condition to the actual commands to be executed under the control statement, but why were it chosen to use both semicolon and a keyword? For example if condition ; then do-something…
skyking
  • 498
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
  • 4,573
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…
Chris Davies
  • 130,320
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
  • 4,573
1
2 3
…
14 15