Bash assign string to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash assign string to variable
# 15  
Old 02-03-2011
Quote:
Originally Posted by cfajohnson
There is no good reason for using let. It is not standard, and it adds nothing.
whether there is good reason or not, technically speaking, its still a valid way to do maths. Some may like do things this way
Code:
$ m=0
$ let "m=m+1" # or let "m+=1"
$ echo $m
1

and some may like it this way too
Code:
$((m++))

In the end, its just a matter of choice. Isn't it?


Speaking of not standard, a lot of things pertaining to different shells are not standard either.
# 16  
Old 02-03-2011
Quote:
Originally Posted by ghostdog74
Code:
$((m++))


That's not portable (i.e., POSIX); it is not a valid Unix shell statement.
It may work in some shells, but there's no guarantee.

The reliable way is:
Code:
$(( m += 1 ))

Quote:
In the end, its just a matter of choice. Isn't it?

No. There is no good reason to write unportable syntax when the portable is just as efficient.
Quote:
Speaking of not standard, a lot of things pertaining to different shells are not standard either.

True, and they should only be used when the portable method is less efficient.

The vast majority of scripts can be written using only POSIX syntax.
# 17  
Old 02-03-2011
Quote:
Originally Posted by cfajohnson

That's not portable (i.e., POSIX); it is not a valid Unix shell statement.

that's my illustration of being not standard, but yet is also a valid syntax provided by bash.

Quote:
The reliable way is:
Code:
$(( m += 1 ))

don't work in bourne shell

Quote:
No. There is no good reason to write unportable syntax when the portable is just as efficient.
not talking about portable or not portable here. Bash provides the facility to write non portable code such as the syntax I described, but are you going to stop every user who bash not to write it that way?
Quote:
True, and they should only be used when the portable method is less efficient.
efficient? who cares (except cfajohnson)? are you going to next show me proof that let "m+=1" is slower or faster the $((m+1)) ?

Quote:
The vast majority of scripts can be written using only POSIX syntax.
If there are only one type of shell in this world, who needs a POSIX standard? while I do not disagree with you on this, not all scripters want to write in POSIX mode as well. A vast majority of them prefers to utilize their shell to the fullest to get the job done.
# 18  
Old 02-03-2011
Quote:
Originally Posted by ghostdog74

that's my illustration of being not standard, but yet is also a valid syntax provided by bash.

IOW, it is limited to bash. Why do that when you can write in a way that works in bash as well as all other POSIX shells?
Quote:

don't work in bourne shell

Nor does any other form of arithmetic. But the Bourne shell is not a standard Unix shell (it is a precursor to it).
Quote:

not talking about portable or not portable here. Bash provides the facility to write non portable code such as the syntax I described, but are you going to stop every user who bash not to write it that way?
efficient? who cares (except cfajohnson)? are you going to next show me proof that let "m+=1" is slower or faster the $((m+1)) ?

It is much faster when you have to rewrite your script for a standard shell that doesn't support that extension.
Quote:
If there are only one type of shell in this world, who needs a POSIX standard?

There are many shells; that's why the standard is important.
Quote:
while I do not disagree with you on this, not all scripters want to write in POSIX mode as well. A vast majority of them prefers to utilize their shell to the fullest to get the job done.

I, too, utilize the shell to the fullest. The way I do that is to use the most portable syntax available. When I need something that is not available in the standard shell, then and only then do I use the more parochial syntax.
# 19  
Old 02-03-2011
Quote:
Originally Posted by cfajohnson
IOW, it is limited to bash. Why do that when you can write in a way that works in bash as well as all other POSIX shells?
if you are talking about using let, I personally would not use it, but i don't condone it either if its used appropriately. In this world, not all want to write in POSIX mode. Why? If I am using bash 4 and I want to change case for example, I want to use $^ or $, instead of calling external commands since they are internals and I don't have to use it in other shells... does that answer why?
take note, that we are OT already. My only comment is that "let" is technically correct and legal to be used in OP's context and not a question of writing POSIX or not.
Quote:
Nor does any other form of arithmetic. But the Bourne shell is not a standard Unix shell (it is a precursor to it).
You said the more reliable way is $((m+=1)). I said it does not work in bourne shell. But if you want to talk about POSIX and stuff and if I am still using the bourne shell or tcsh, then it will not work right? In any case, this is also OT
Quote:
There are many shells; that's why the standard is important.
I know why the standard comes about. You did not get my point and I am too lazy to explain my point to you either.
Quote:
I, too, utilize the shell to the fullest. The way I do that is to use the most portable syntax available.
When I need something that is not available in the standard shell, then and only then do I use the more parochial syntax.
so just to get this clear, you do write in a way that is usable in bourne shell, zsh , tcsh , dash, etc, correct?


# 20  
Old 02-03-2011
Quote:
Originally Posted by ghostdog74
if you are talking about using let, I personally would not use it, but i don't condone it either if its used appropriately. In this world, not all want to write in POSIX mode. Why? If I am using bash 4 and I want to change case for example, I want to use $^ or $, instead of calling external commands since they are internals and I don't have to use it in other shells... does that answer why?

Isn't that what I said? When I want to do something that is more efficiently done using a non-portable syntax, I do that. (BTW, you can thank me for suggesting the ${var^} and ${var,} expansions to Chet Ramey.)

However, if I am unable to use bash4 (it is not installed on some systems I use) I don't use external commands for those conversions; I use functions that use only the shell.
Quote:
...
so just to get this clear, you do write in a way that is usable in bourne shell, zsh , tcsh , dash, etc, correct?

I write, as much as possible, in a way that will run in any standard Unix shell (that is, a POSIX shell).
# 21  
Old 02-03-2011
Whatever it is, the OP did not say he wants or not to do POSIX mode. The title also specifically said "Bash".
Using "let" the way he uses it is not "wrong" either, since its supported in Bash, (although I do agree it's not necessary.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using read to assign value to bash variable not working

Hi, I am attempting to assign the output of the following command, to two bash variables, var1 and var2 using "read," but it doesn't seem to be working. # openstack hypervisor stats show | awk -F'|' 'NR==14{print $2,$3}' vcpus 92 # echo $? 0 # openstack hypervisor... (4 Replies)
Discussion started by: sand1234
4 Replies

2. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

3. Shell Programming and Scripting

Assign variables to CSV string (bash)

Hi guys, New to the forum, and been messing around with Linux for about a year now. I'm still very much a rookie, so just assume that I'm a total idiot: I currently have a shell that spits out a CSV number string of about 8 numbers as follows: 1.00,2.00,3.00 ... ,8.00I need to assign a... (7 Replies)
Discussion started by: hansol
7 Replies

4. Shell Programming and Scripting

Assign a variable the nth character of a string.

I see a millioin ways to do this with echo, but what I wan to do is assign a variable the "nth" character of an incoming parameter to a ksh script. $1 will be "pia" I need to assign the first character to stmttype. (10 Replies)
Discussion started by: klarue
10 Replies

5. Shell Programming and Scripting

need to assign a string to a variable

Hello Experts, In my script i am using the below syntax /usr/bin/head -1 /opt/chumma.txt | /usr/bin/cut -d " " -f3 output of this one is --> Monday I want to store this String in a variable (i.e) j or k... Give me some idea experts. Thanks in advance. (7 Replies)
Discussion started by: natraj005
7 Replies

6. Shell Programming and Scripting

bash assign mysql query single field to variable

I'm running a bash script query and assigning the output to a variable like this: exists=`mysql -u $USER_NAME --password=$PASSWORD -D "somedb" \ -e "SELECT * FROM somedb.sometable WHERE field1 ='$a' \ AND field2 ='$b' LIMIT 0 , 30";` which returns something like: echo... (2 Replies)
Discussion started by: unclecameron
2 Replies

7. Shell Programming and Scripting

assign awk output to bash variable

greetings all, I am have a heck of a time trying to accomplish a very simple thing. I have an array of "shortname<spaces>id" created from a dscl output. I want to assign shortname=word1 and id=word2. I have tried shortname=$(${textArray} | awk '{print $1}') - and get 'awk : cannot open... (3 Replies)
Discussion started by: macnetdaemon
3 Replies

8. Shell Programming and Scripting

Assign bash command to variable

Hi I am trying to write a function that needs to be able to assign the last run shell command to a variable. The actual command string itself not the exit code of the command. I am using the bash command recall ability to do this as follows: alias pb='ps | grep ash' ... (3 Replies)
Discussion started by: Moxy
3 Replies

9. Shell Programming and Scripting

read and assign each character from the string to a variable

How... can I read input by a user character by cahracter. And assign each character from the string to a variable? Any help would be greatly appreciated! Thank you! (1 Reply)
Discussion started by: Tek-E
1 Replies

10. Shell Programming and Scripting

How to assign variable from a string having blanks in between

Hi All, I am new to bash scripting. I need your help to removing spaces from a string and assign them to different variables. Iwant to call script with one command line argument which is file name which containes different attributes their type and different values eg ... (1 Reply)
Discussion started by: flextronics
1 Replies
Login or Register to Ask a Question