Any bash scripters here?

Any bash scripters here?

I have this function

mult () {
local sum
let "sum=$1*$2"
return $sum
}
mult 2 4
echo $?

Great, it gives me eight, but what is happening when the make the arguments expressions

mult () {
local sum
let "sum=$1*$2"
return $sum
}
mult 1+2 9+3
echo $?

I don't understand the answer im getting, which would be 22 in this case.

Attached: Reginald_Barclay,_2366.jpg (350x311, 14K)

Other urls found in this thread:

mywiki.wooledge.org/BashGuide
twitter.com/SFWRedditImages

>sum=1+2*9+3
>sum=1+18+3
>sum=22

just use python

fuck thank-you dude lol

Attached: barclay.jpg (694x530, 89K)

What a shitty sidekick character he was. Hated him almost as much as that curly hair guy. Was his name O'Brien?

>reading is hard

What the actual fuck

I see what its doing, but why the fuck is it interpreting 1+2 as the whole argument instead of interpreting it as 3? For what purpose?

bash doesn't do math on the command line...

if you type 1+2, you get "1+2: command not found". it passes these arguments as strings, so you end up doing string concatination in the let statement:

let "sum = 1+2*9+3"

and THEN it does the math eval

>basically a mini interpreter that then sets environment variables to its own environments variables
wtf is wrong with the shell creators

Why would you expect that? Bash never evaluates arithmetic expressions unless you tell it to.

echo $(( (1+2)*(9+3) ))

>why

Why does it treat those strings at integers when multiplying them, but the string is treated like a string when used as an argument

dumb barclay posters

Arithmetic happens inside of $(( )).
Don't use retarded bashisms for no reason.

>Why does it treat those strings at integers when multiplying them
Because there's no such thing as string multiplication. Multiplication is always on ints.

>the string is treated like a string when used as an argument
There's no such thing as an integer argument. They are always strings.

While some of you may be, anyone mind explaining to me why this gives an output of 2 (with an error message)


test a -gt b
echo $?

Attached: Reginald_Barclay_with_Albert_Enstein.jpg (694x530, 88K)

may be here*

That's because of the error. A false expression would return 1. Return values greater than 1 indicate an error.
And the error gets thrown because a numeric test expects integers.

Are you fucking retarded? Go read a bash tutorial.

mywiki.wooledge.org/BashGuide

Thanks
Maybe

Attached: kinotemple.jpg (1920x1080, 162K)

let

What idd you expect op, this?

env -i bash

my bad, that should be val='one * two'

Is there any good solution for writing portable shell scripts which doesn’t require writing a shell script?
Like a compiler from a sane language to sh?

not that I'm aware of.

as someone who unironically knows shell scripting all too well, my best advice is to write everything "shell oriented" in awk and then do system() or 'your shell string ' sh_escape(arg) ' blah' | getline when you need to do something the awk interpreter doesn't expose to you. awk is shitty as hell, doesn't even have first class array initialisms, regexps, etc but at least it's a normal ass sane language with basic string handling and functions.

>sane language to sh
The bourne shell is fine, the people who use it are often retards though. see:

#!/bin/sh

awk

never fucking mind im retarded

also, honestly. you can just pull an "ansible" and just require python3 to be installed on the targets you script against.

python's standard library is massive and would deprecate the need to use garbage languages like shell and the mess of coreutils altogether.

Except that your entire OS(POSIX) depends on the bourne shell.

no, it doesn't. I can make a linux distro that boots an init that isn't /bin/sh and never have it installed. if the shitty program shells out ever, then I'd just edit the source and fucking remove its use.

You can't even implement exec() without /bin/sh

what are you on about moron? maybe man 2 execve and fuck right off. it is very possible to have a "poosix" system running without /bin/sh if so desired.

I asked for the curly hair guy...

seems like retarded idea anyway, why would you wanna do this?