Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [syntax]

The tag has no usage guidance.

48
votes
3answers
7k views

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 ...
35
votes
6answers
6k views

Is ~/Mary a relative path? [duplicate]

Let's suppose Mary is a directory. Is the following path ~/Mary relative?
27
votes
4answers
3k views

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 || ...
16
votes
1answer
2k views

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 ...
14
votes
2answers
63k views

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 ...
11
votes
6answers
2k views

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 ...
11
votes
1answer
2k views

What does !!: mean in bash? [duplicate]

Watching a video (https://vimeo.com/56166857 & https://www.youtube.com/watch?v=9CTfGS0gEOk @ 1m:34s), the presenter (TJ Holowaychuk) wrote: touch !!:2{...} I can't find what !!:2 means anywhere. ...
8
votes
2answers
513 views

When is it useful to use “grep — SOMEPATTERN”?

Under Linux/AIX where ever, when is it useful to use --, as in this command: grep -- SOMEPATTERN
8
votes
2answers
1k views

How to export a large list of variables in Bash? [duplicate]

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 ...
6
votes
2answers
687 views

Bash brace expansion to remove part of filename

Is it possible to remove rather than adding substring to a filename using bash brace expansion? Considering the following scenario, one can add a suffix to a filename by using the below technique: ...
6
votes
4answers
1k views

back ticks vs double quotes

I've been wondering this for a long time but haven't figured out how to look it up - is this: x=`command -v r2g` the same as this: x="$(command -v r2g)" or is it the same as this: x=$(command -...
5
votes
2answers
745 views

Why is `source ~/.bash_aliases` exiting with syntax error?

What is the syntax error in this file? I can't spot it. set-k8s-azure() { export KUBECONFIG=~/.kube/config.azure-1 } set-k8s-minikube() { export KUBECONFIG=~/.kube/config.minikube } minikube() {...
5
votes
2answers
271 views

If [ is a builtin and [[ is a keyword, then what is ((?

According to this post, [ is a builtin and [[ is a keyword. Since they can be regarded as command, everything after [ and [[ is regarded as command arguments and should be separated by spaces, until ...
5
votes
1answer
1k views

null string as command in while loop: empty/null-command

I'm wondering how the while loop evaluates its loop condition. In the man page it is written: while list-1; do list-2; done The while command continuously executes the list list-2 as long as ...
4
votes
3answers
575 views

Correctly defining a Cron schedule for process to be run every hour

There is a record: 45 * * * 1 script.sh and 45 0-23 * * 1 script.sh The desired effect is to run the script 45 minutes after every hour on Mondays. Are they identical? If not, what is the ...
3
votes
5answers
169 views

How to execute many files in the same directory in a minimal, DRY, pretty way?

I use Ubuntu 16.04 and I have a localization file, a file that executes many files, that I got after downloading my own GitHub project to my machine. This file contains a Bash script, and is named ...
3
votes
2answers
69 views

Is the synopsis section of the man page for the find command wrong?

So, I have been trying to get into the habit of first looking at man pages before starting to google when I forget how to run a certain command. I was looking at the man page for the find command ...
3
votes
2answers
559 views

How to write regexp literal in match expression?

This question is about the proper way to write regular expression literals in a match expression under bash. In zsh, the match below succeeds, as I expected: % [[ ' 123 ' =~ '^ [0-9]+ $' ]]; echo $? ...
3
votes
3answers
455 views

Can a function in sh have zero statements?

Are there any relevant standards that dictate what an implementation of sh must do with an empty function? The following snippet defines a function with zero statements a() { } The subshell version ...
3
votes
1answer
2k views

The repeat command syntax in zsh

I'm beginning with zsh and I use this manual. The syntax for repeat is presented as: repeat word do list done However I've noticed that: repeat 5 echo ok also works fine. Still, this omission ...
3
votes
1answer
73 views

Using a bash array in an awk and also quoted variable: conflicting syntax issue

I have a script the aim of which is: For a list of files, obtain a specific number for each file (this is sequencing data, to be specific) and store these into array1 Using array1, find the smallest ...
3
votes
1answer
162 views

Why ssh immediately disconnects when using -F /dev/stdin syntax?

I've configured and booted default Ubuntu VM box by the following commands: vagrant init ubuntu/trusty64; vagrant up --provider virtualbox Now, for education purposes, I'd like to use the following ...
3
votes
1answer
244 views

Breaking long lines with comparison operators in a shell script

in my bash code sometimes I use long parameters in Comparison Operators to test the "OR" as the following: if [[ $production_check_linux_disks_quantity == false ]] || [[ $...
3
votes
1answer
526 views

Meaning of the double dash in options [closed]

I've read that all multi-character command options must be preceded by a double dash (--). But many flags for the find command (e.g. -name or -type) are preceded by only one dash. Why is that?
3
votes
1answer
86 views

Have remind send out notifications some days in advance

I have been trying to use the Unix program remind, and so far I enjoy the flexibility. I have set up this command to warn me of any upcoming reminders: ~$ remind -z -k'notify-send "Reminder" "%s"' ....
2
votes
2answers
2k views

Syntax error near unexpected token 'else'

I'm trying to create a script that will remove certain parts of a txt file full of status info and other various text. I can't end the main if statement. Also I'm trying to get it to delete the ...
2
votes
2answers
335 views

From multiline heredocument to a uniline herestring, with line breaks

I have this multilinie heredocument which I desire to translate into a uniline herestring: cat <<-"PHPCONF" > /etc/php/*/zz_overrides.ini [PHP] post_max_size = 200M upload_max_filesize ...
2
votes
1answer
234 views

syntax error in For loop script

I'm new in Linux and I also know this is a pretty common problem in the forum's questions but I have tried several options and I can't solve this issue. I am trying to execute the following script: !...
2
votes
1answer
145 views

Is it a best practice to use Ansible-Galaxy roles in playbooks instead ansible-modules in playbooks? [closed]

I have an Ansible playbook for establishing LAMP environments on remote machines. Part of this playbook deals with installing Composer: - name: Install Composer get_url: url: https://...
2
votes
2answers
160 views

How can I use “source-highlight” with “git show”?

Using source-highlight in conjunction with less works really well, but I'm struggling to see how I can use it with git show because there's no file extension to clue source-highlight about the ...
2
votes
1answer
51 views

How many different variations of this command?

That's an unusual question. Let's say I have one command with this synopsis: /usr/bin/wc [-c | -m | -C] and second command with head [-number | -n number] [filename] The question is how many ...
2
votes
0answers
32 views

How to maintain a list of names for the Hunspell spell checker?

Hunspell should help the user to write names properly. The names should be defined in the home directory of the user, so that every user can define his or her own list of important names. If a user ...
2
votes
0answers
73 views

What are the contexts where Bash doesn't perform word splitting and globbing?

Bash doesn't perform word splitting in globbing in these cases: LHS or RHS of an assignment, except for indexed arrays var=$value # simple variable declare -A hash key=...
2
votes
0answers
34 views

Exclude subdirectory from zip archive — explain syntax please

I want to zip a folder recursively, but exclude certain directories (temp/.hidden in the example below). I want the zip file to have a top level folder which would be temp in the example below, hence ...
1
vote
1answer
401 views

List of acceptable initial characters for a bash variable

What are the acceptable characters in a bash variable? Can someone point me to the place in the bash man page that describes the acceptable name for a bash variable? I looked and came up with a lot ...
1
vote
1answer
3k views

shell script error : Syntax error: “(” unexpected

I have checked the previous post on this error. still did not get the solution working. Here is my bash script. can some one help me figure out the issue . I have used https://www.shellcheck.net/ to ...
1
vote
1answer
4k views

Running R script via shell script. syntax error near unexpected token `('

I am currently trying to run an R script via a shell script. Here the R script: test = rnorm(1:100, 1000) write.csv(test, 'test.csv') And here the bash script which calls the R one: #!/bin/bash -l ...
1
vote
2answers
66 views

How does '-' work in bash redirection?

I have seen this explained somewhere before but can't find a reference now. An example usage from docker getting started guide for Ubuntu: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | ...
1
vote
1answer
7k views

syntax error near unexpected token `do

I have a "syntax error near unexpected token `do" error, using a for loop in a .sh. Here is the code : #!/bin/sh # some code for flux in $(ls -d /home/eai/*/*/*) ; do FICHIER=$(ls -p -tr $flux | ...
1
vote
1answer
97 views

Concerning AWK programming, what do the -f switch and ~ operator do? [closed]

I can't seem to find anything on these switches. I know the "-F" option can be used to change the delimiter but that's the extent of what I've found other than it looks like maybe a compiling option; ...
1
vote
1answer
67 views

How to Define Custom Conditional Expression Primaries in Bash?

How would I redefine and/or define new conditional expression primaries in bash? For example, I would like a primary that tests for the existence of an empty directory. Of course, it would be ...
1
vote
1answer
90 views

Is it a best practice to wrap paths in quotation marks?

Consider the following code: for domain; do cat << EOF > "/etc/nginx/sites-available/${domain}.conf" My \domain is ${domain}. EOF done Note that the path /etc/nginx/sites-...
1
vote
1answer
649 views

use loop variable for calculation bash

for i in {0..9} do T=$(bc<<<"8+$i*0.5") echo $T done I get : syntax error near unexpected token `T=$(bc<<<"8+$i*0.5")' I believe the problem is the $i. What am I doing ...
1
vote
2answers
230 views

AWK usage for simulatenous inline division and taking absolute value

I have a data file of this format (top row is for reference): E T N D S s R k 0.45847 300.0000 9....
1
vote
1answer
125 views

Hyphen (-) ignored in the end of a piped command nested inside a function?

I use Ubuntu 16.04 (xenial) with Bash 4.3.48(1). Based on the answer of Stephen Kitt in this session, I tried run the following command as part of a function named imb(): mysqldump -u root -p --all-...
1
vote
1answer
191 views

Basic usage of /etc/cron/ (d): correct pattern for /etc/cron (daily/weekly/monthly)

I use Ubuntu 16.04 with Bash and I've created this extensionless, shebangless file /etc/cron.daily/cron_daily: for dir in "$drt"/*/; do if pushd "$dir"; then wp plugin update --all --allow-root wp ...
1
vote
2answers
226 views

Syntax for `repeat` with a semicolon in the command string?

I'm keeping an eye on a process in FreeBSD using a script which outputs the current status of the job. Using the csh builtin command repeat, I'd like to run the script every 2 seconds, so naively I'd ...
1
vote
1answer
1k views

Syntax error near unexpected token '{'

Every time I run new virtual terminal, it starts with bash: /usr/share/bash-completion/bash_completion: line 226: syntax error near unexpected token `}' bash: /usr/share/bash-completion/...
1
vote
1answer
99 views

Dash Slash Meaning vs just Slash -/var vs /var [duplicate]

Working with Centos 7 In the rsyslog.conf file there are some rules that place logs in directories that start with a -/ (Dash slash) before stating which directory to place the logs, while others ...
1
vote
1answer
61 views

How to use one path in heredocument

I wrote this here-document to source a few scripts under the path ~/own_scripts/ but I wrote it in a way that causes duplication of this path: source <<-EOF ~/own_scripts/1.sh ~/own_scripts/...