Explaining behaviour of sudo bash "$0" "$@";

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Explaining behaviour of sudo bash "$0" "$@";
# 8  
Those are round braces, not curly braces, and they create a sub-shell. Right now their only side-effect is wasting time and memory, but if you make a habit of that you'll discover that variable assignments and the like don't communicate outside them.

So they're not decorations and can cause problems if you make a habit of it.
This User Gave Thanks to Corona688 For This Post:
# 9  
Bash programming is a true headache for beginners. May you suggest any reference book that has this kind of explanations if there is any? Conventions, best practices?
# 10  
Conventions and best practices are to avoid inventing new syntax to make it "look better". Don't do that. Every time I've done that I've come to regret it when it fails to translate to some other shell.

Never paste shell code into a word processor. It will convert all your quote characters into nasty smart quote characters which to the shell don't count as quotes at all.

Arithmetic is best done inside RESULT=$((VAR+37)) statements instead of archaic let statements.

Numerical comparisons are best done inside if (( VAR1 > VAR2 )) statements instead of the archaic if [ "$VAR1" -gt "$VAR2" ]

String comparisons are still done inside if [[ "$STR" == "$STR2" ]] statements, note the doubled [[ ]]. The single [ ] mostly work the same but are archaic.

Backticks a la VAR=`command` are also archaic. To capture the output of a command, use VAR=$(command) instead.

Never use the $? special variable if you can possibly help it. If you're trying to use it, there's probably a logic structure that would work better, such as if, while, or case.

You can always plug a command into an if/while statement where you'd put a ((math)) or [[ bracket ]] statement, because to the shell they're mostly the same thing. case is different, case operates on a string.

Whitespace is unimportant inside (( )) brackets, but anywhere else, whitespace is crucial. This is wrong:

VAR = "value"

This is right:


Single quotes ' and double-quotes " can both be used to denote strings but operate differently. Any text inside single quotes is not evaluated, where text inside double-quotes has variables substituted inside.

If you manage to force quote characters to be stored in a variable, they are not processed specially by the shell any more, they're just characters.

Avoid making too-long command | command | command | command | command chains. They are inefficient.

Avoid using external commands to process single strings, like VAR=$(echo "a b c" | awk '{ print $1 }') There's usually a shell builtin that will do a better job without the overhead of making an entire external program / language load, process, print, and quit.

The advanced bash programming guide is a good place to look things up since nobody really had everything memorized.

Last edited by Corona688; 11-06-2018 at 01:22 PM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #670
Difficulty: Medium
Fourteen channels are designated in the 2.4 GHz range, spaced 5 MHz apart from each other except for a 12 MHz space before channel 14.
True or False?

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

Expect: spawn id exp5 not open while executing "expect "$" { send "sudo su -\r" }"

Hi All, i am trying to ssh to a remote machine and execute certain command to remote machine through script. i am able to ssh but after its getting hung at the promt and after pressing ctrl +d i am gettin the out put as expect: spawn id exp5 not open while executing "expect "$" {... (3 Replies)
Discussion started by: Siddharth shivh
3 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

6. Solaris

The slices "usr", "opt", "tmp" disappeared!!! Help please.

The system don't boot. on the screen appears following: press enter to maintenance (or type CTRL-D to continue)...I checked with format command. ... the slices "0-root","1-swap","2-backup" exist. ...the slises "3-var","6-usr" -unassigned. :( (16 Replies)
Discussion started by: wolfgang
16 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies