HarryL44
Posts: 9
Joined: Thu Mar 09, 2017 8:21 am

Quick bash Question

Mon Mar 13, 2017 5:35 am

In an if statement when writing a bash script.

Why do you need

Code: Select all

$value -lt 1 
for the condition
but for an assignment operation and read you only use

Code: Select all

value=1 
or

Code: Select all

read value
(Notice the lack of '$')

Is the '$' symbol only used in comparison? Cant seem to find the answer i need in google :P

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: Quick bash Question

Mon Mar 13, 2017 5:54 am

Because shell, at its basic level, is a textual replacement language.

I.e., in most higher level languages, when you write:

foo

you mean "the value of foo". In shell, when you write:

foo

you mean "foo". To get the value of foo, you need to write:

$foo

which means to textually substitute the value of foo into the line of text at that point.

Other languages (e.g., perl) do things differently. Also, more advanced shells (e.g., bash) have certain constructs where you can leave off the $. But, most of the time, for the basic stuff that is common to all "shells", you need $ when you want the value of the variable, not just the literal text.

The upshot of all this is that when you write:

foo=3
if [ $foo -lt 5 ]

the line that actually gets executed is:

if [ 3 -lt 5 ]

Oh, also, if you write:

$foo=3

that is a syntax error. You can try this (at your own risk):

foo=bar
eval $foo=3

which sets bar to 3.

It all makes sense if you think of shell as a simple textual replacement system, rather than as a programming language...
If this post appears in the wrong forums category, my apologies.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Quick bash Question

Mon Mar 13, 2017 6:31 am

HarryL44 wrote:Why do you need

Code: Select all

$value -lt 1 
but for an assignment operation and read you only use

Code: Select all

value=1 
:P
Because bash is insane, ask anybody.

In powershell, for example, you just use the leading $ to indicate a variable. Bash has a lot of really weird things that you will just have to write down in your notes and try not to screw up. For example, you should put quotes, a brace of ", around all variables, expect when you shouldn't, say with:

Code: Select all

wally="jimmy bobby freddy jerry"
for x in $wally; do
  echo "$x"
done
Note the lack of a space in the assignment to wally. Why does the x in for not have the $? What happens when you put "" around the $wally compared to otherwise? And try to add one to some variable. There are like 15 different ways to do it and none of them make a lick of sense. Like I said, insane.

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: Quick bash Question

Mon Mar 13, 2017 6:49 am

Because bash is insane, ask anybody.
First of all, when you write it like that, it makes it sound like you are singling out bash in particular. But actually, most of bash is inherited from the general class of "Bourne-ish shells" - so it doesn't make sense to imply that bash is any more or less sane than the rest of the shells in that class. And, in fact, that class of shells is more sane than most (all?) of the others. Sorta like what Churchill said about 'democracy'.

Second, you only get trouble when you start thinking of (Bourne-ish) shell as a programming language and start comparing it to "real" programming languages (like Perl, Powershell, etc)

Third, bash is actually more like a "real" programming language than its predecessors - which is mostly a good thing, but leads to lots of confusion over whether it is or isn't a programming language. zsh is even worse in this regard...

Finally, I like your comment about 15 different ways of incrementing a variable. You got that right.

P.S. Another language that is in this "Is it a programming language or is it just a simple macro/text-substitution system?" area is Tcl. That's a strange one to learn... I've been using Tcl (in the context of Expect) for decades and I still don't really get it.
If this post appears in the wrong forums category, my apologies.

k-pi
Posts: 930
Joined: Sun Feb 12, 2017 1:46 pm
Location: Upper Hale, Surrey, UK.

Re: Quick bash Question

Mon Mar 13, 2017 12:56 pm

Most of unix shells were designed to use the small programs that makes up the base system.
They were coded to be used 'chained' together.
e.g.

Code: Select all

dmesg | grep network

or

Code: Select all

ls | sort > sorted.lst

wayne.dolesman
Posts: 64
Joined: Sat Feb 25, 2017 8:10 am

Re: Quick bash Question

Mon Mar 13, 2017 1:34 pm

One way to look at it is that variables have two properties. Their name and their contents.

Is the '$' symbol only used in comparison? Cant seem to find the answer i need in google :P
$ is a way to reference the contents. Without it you are referencing the variable.

basket = apples; aka The basket contains apples.
$basket references its contents (the apples).
basket references the container where you can put things.


Code: Select all

$value -lt 1 
use the contents of "value"

Code: Select all

value=1 
Use the variable name not the contents.

Code: Select all

read value
Use the variable name not the contents.

Does this help understand it?

wayne.dolesman
Posts: 64
Joined: Sat Feb 25, 2017 8:10 am

Re: Quick bash Question

Mon Mar 13, 2017 1:39 pm

Martin Frezman wrote:But actually, most of bash is inherited from the general class of "Bourne-ish shells"
The newest in the saga, The Bourne Shell starring Matt Daemon (cause he runs in the background) :)

jahboater
Posts: 5680
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Quick bash Question

Sat Mar 25, 2017 7:13 pm

Martin Frezman wrote:
Because bash is insane, ask anybody.
First of all, when you write it like that, it makes it sound like you are singling out bash in particular. But actually, most of bash is inherited from the general class of "Bourne-ish shells" - so it doesn't make sense to imply that bash is any more or less sane than the rest of the shells in that class. And, in fact, that class of shells is more sane than most (all?) of the others.
Yes, and the Bourne shell was around when Windows/DOS users were stuck with command.com and cmd.exe - which are appalling.

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: Quick bash Question

Sat Mar 25, 2017 8:34 pm

Yes, and the Bourne shell was around when Windows/DOS users were stuck with command.com and cmd.exe - which are appalling.
Windows/DOS users are *still* stuck with CMD.EXE (COMMAND.COM is long gone).

Yes, I know there's powershell, but nobody uses that, do they?

And various things based on VBA, and so on. But again, nobody really uses them much.

My point in all this is that if you're going to do actual programming (as opposed to shelling - which is what all these shells, to one degree or another, are), then you might as well use a real programming language.
Note: You may disagree with this, but that really doesn't concern me.
If this post appears in the wrong forums category, my apologies.

User avatar
r3d4
Posts: 982
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: Quick bash Question

Sat Jul 15, 2017 2:20 pm

see also

Code: Select all

man test
man test wrote: INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
:arrow: https://en.wikipedia.org/wiki/Test_(Unix)
wiki/Test_(Unix) wrote:The test command in Unix evaluates the expression parameter. In most recent shell implementations, it is a shell builtin, even though the external version still exists. In the second form of the command, the [ ] (brackets) must be surrounded by blank spaces. This is because [ is a program and POSIX compatible shells require a space between the program name and its arguments.
https://learnxinyminutes.com/docs/bash/

Return to “General discussion”