Bash Scripting for Beginners: Complete Guide to Getting Started - Basic Math (Part 4)

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.พ. 2025

ความคิดเห็น • 50

  • @coolmanabc1231
    @coolmanabc1231 ปีที่แล้ว +12

    I hope your business keeps growing. I appreciate you man.

  • @helloimatapir
    @helloimatapir 2 ปีที่แล้ว +76

    "expr" is good to know, but on a modern system, you should be using "$(( ))" for numeric calculations. E.g. $((a + b)). Also, for multiplication you can simply do $((10 * 10)), not needing a double asterisk or escape character.

    • @nerfherder4284
      @nerfherder4284 2 ปีที่แล้ว +10

      You literally just helped me fix a script I was writing to convert GPU temps from C to F. Thanks! 👍🏻

    • @gfjffdgjroedsfdkdslmerrelb4357
      @gfjffdgjroedsfdkdslmerrelb4357 ปีที่แล้ว +2

      i am looking for partner who want to learn bash through co opearation

    • @VitaDani314
      @VitaDani314 ปีที่แล้ว +1

      If I do it this way, it performs that calculation, but it is also treating it as a command

    • @imnottellingyoumyname3050
      @imnottellingyoumyname3050 ปีที่แล้ว +1

      @@VitaDani314 same. Got to echo it first.

    • @marianostone2285
      @marianostone2285 ปีที่แล้ว

      thks bro
      ​@@imnottellingyoumyname3050

  • @DhanushkaPereraDC
    @DhanushkaPereraDC 2 วันที่ผ่านมา

    clear explanation. 🥰

  • @chiomachukwuma2859
    @chiomachukwuma2859 ปีที่แล้ว

    I always love your mode of teaching, very simple, clear and straigth forward. Thanks.

  • @amitkrmandal9974
    @amitkrmandal9974 9 หลายเดือนก่อน +1

    Wow Sir Nice . You have Brilliant Knowledge of Linux. I appreciate you Sir . I am Amit From India .

  • @patrob3885
    @patrob3885 ปีที่แล้ว +3

    Thank you for your course, it is absolutely amazing that I can understand you the way you teach thank you !

  • @kawa_kxf4509
    @kawa_kxf4509 10 หลายเดือนก่อน

    Hello from Athens Greece, I watch your channel and I suggest it to all new people that afraid of using Linux OS.

  • @seta16
    @seta16 ปีที่แล้ว

    10/10 good passed short videos and perfect if someone come from any programming languages to make a reference

  • @christianrobert7915
    @christianrobert7915 2 ปีที่แล้ว +11

    Fun things to try:
    echo {1..30}
    echo {30..0}

  • @monjurrahman8688
    @monjurrahman8688 ปีที่แล้ว

    I am learning many things from your video.
    wish your good health

  • @臧金玉
    @臧金玉 หลายเดือนก่อน

    Thanks for your work

  • @CIDC-4500
    @CIDC-4500 ปีที่แล้ว

    learned alot from your previous Vid and this as well. Thanks much

  • @Bacon0079
    @Bacon0079 หลายเดือนก่อน

    Thanks

  • @franciscoromogaray3076
    @franciscoromogaray3076 ปีที่แล้ว

    classic banger ma bro, short but good

  • @VitaDani314
    @VitaDani314 ปีที่แล้ว

    I noticed that if you surround the * with quotation marks, that works as well

  • @fabiobianchi3104
    @fabiobianchi3104 ปีที่แล้ว

    Great class!!!

  • @barkhadibraahim1023
    @barkhadibraahim1023 9 หลายเดือนก่อน

    thanks jay

  • @VideosRichy
    @VideosRichy 8 หลายเดือนก่อน

    thank you!!

  • @ushousewatch
    @ushousewatch 11 หลายเดือนก่อน +2

    Does bash do order of operations

    • @knoxduder
      @knoxduder 10 หลายเดือนก่อน

      Great question.

  • @13thravenpurple94
    @13thravenpurple94 ปีที่แล้ว

    Great video Thank you

  • @leonel_carrizo
    @leonel_carrizo 2 ปีที่แล้ว

    Thank you!

  • @kainaatmakhani6550
    @kainaatmakhani6550 2 ปีที่แล้ว

    Nice lecture

  • @unbekannter_Nutzer
    @unbekannter_Nutzer 2 ปีที่แล้ว +8

    Oh, no! `expr` is deprecated - since 199x?
    Use $((...)) in bash for simple integer calculations:
    for op in + - \* / % ; do echo $((30 op 10)); done
    40
    20
    300
    3
    0
    Inside the double parens, you needn't mask the asterisk (here it needed to be masked for the for loop).
    You needn't use $ to dereference value variables (I needed, because it was used for the operator
    🐧> v=7
    🐧> echo $((v + 4))
    11
    You may extend the math portfolio with exponentiation, often seen in shells as a^b, but here it is $((a**b)).
    You may even perform bitwise OR, AND and XOR:
    t530:~/dl 🐧> echo $((v | 4))
    7
    t530:~/dl 🐧> echo $((v & 4))
    4
    t530:~/dl 🐧> echo $((v ^ 4))
    3
    For floating point, you need of course, like viesic pointed out, bc (but not for +*/= and reasonable small values).
    t530:~/dl 🐧> echo $((2**62))
    4611686018427387904
    t530:~/dl 🐧> echo "scale=314; 4*a(1);" | bc -l
    3.141592653589793238462643383279502884197169399375105820974944592307\
    81640628620899862803482534211706798214808651328230664709384460955058\
    22317253594081284811174502841027019385211055596446229489549303819644\
    28810975665933446128475648233786783165271201909145648566923460348610\
    45432664821339360726024914127372458700660628
    Do a performance comparision between `expr FOO` and `echo $((FOO))` in a loop which runs for 1s. On my system, the bash builtin `$((FOO))` is about 200x faster, than the external `expr FOO` call. Performance often doesn't matter, but there is no single advantage in using `expr` - well, maybe it is more portable for shells, which don't have `$((...))` or something similar.

    • @AlbusRegis
      @AlbusRegis 2 ปีที่แล้ว

      Old versions of bash are still running around on the most unexpected places, and I agree with you that almost always $(()) is superior

    • @unbekannter_Nutzer
      @unbekannter_Nutzer 2 ปีที่แล้ว

      @@AlbusRegis What do you mean by `old`? Do you think an intro to newbies should focus on teaching archaic syntax and styles?
      In the file /usr/share/doc/bash/NEWS.gz, I find the news for version 2.0, following bash-1.14.7.(my current version is 5.1.16), stating (among other things):
      t. The $[...] arithmetic expansion syntax is no longer supported, in
      favor of $((...)).
      Bash 2.0 seems to be from 2001. So most of its users are already dead. :)
      Even the good, old `let` command is superior to `expr`, since it can perform ` let b=30**3; echo $b`

    • @AlbusRegis
      @AlbusRegis 2 ปีที่แล้ว

      @@unbekannter_Nutzer I have found on the wild servers that sould be on a museum, running DOS era software because the big boss could not be bothered to update the program that "works perfectly fine". Unfortunately you have to be at least aware of deprecated stuff since it is out there and very much half alive.

    • @unbekannter_Nutzer
      @unbekannter_Nutzer 2 ปีที่แล้ว

      @@AlbusRegis Well, I don't doubt that some old servers with outdated software is still in use, and for an experienced programmer, it is nice to know, how to handle them.
      These videos are made in a technical satisfying way, well pronounced, fine equipment with a high, reliable frequency. Often I like to nit-pick on small issues, but here we see a lot of work wasted to the wrong audience. Who ever is already working as admin, supporting outdated machines, is a) very probably already trained b) in a small minority.
      It's such a pity if beginners learn bad habits about math in the shell in such a beautiful way.

  • @viesic
    @viesic 2 ปีที่แล้ว +3

    did not know the "expr" command. For mathematical calculations i`m using "bc" command, like that "echo 30 + 10 | bc"

    • @jyvben1520
      @jyvben1520 3 หลายเดือนก่อน

      bc -i goes into interactive mode, nice

  • @doomtm5500
    @doomtm5500 9 หลายเดือนก่อน

    !!!!*DOUBT*
    whenever I create a variable in linux lets say 'myVar' it returns - myVar : command not found..... Can anyone pls tell the solution ??????

  • @zakariabasraoui1580
    @zakariabasraoui1580 ปีที่แล้ว

    thnx!

  • @doug2794
    @doug2794 11 หลายเดือนก่อน

    These math operations only seem to work with integers.

  • @jeffschroeder4805
    @jeffschroeder4805 2 ปีที่แล้ว

    Will a division operation that doesn't reduce to an integer generate a float or maybe truncate the result to an integer? Will the expr command work at all with floats?

    • @AlbusRegis
      @AlbusRegis 2 ปีที่แล้ว +1

      It truncates to nearest low integer: 8/3 will output 2 instead of rounding to 3.
      My advice is that you should not trust bash to handle critical math, it makes good enough aproximations and works well with integers most of the time, but floating point or other number types are finicky at best
      Edit:
      expr errors with non integers:
      expr 8.1 + 1
      "expr: non integer argument"

    • @unbekannter_Nutzer
      @unbekannter_Nutzer 2 ปีที่แล้ว

      @@AlbusRegis IMHO you can't say, it performs approximations. It's absolute accurate (hope so ;) ) in the range and scope of integer arithmetic, which means, it truncates, yes, predicable. 99/100 is zero and in many mature programming languages, if using int, too (C, Java, C++, ...).
      expr 7234534365 \* 14734536 + 977
      106597507044330617
      (in the range up to 2^63-1).
      But
      expr 99 / 100 \* 100
      will give you a result of 0, since it is evaluated left to right.

    • @gfjffdgjroedsfdkdslmerrelb4357
      @gfjffdgjroedsfdkdslmerrelb4357 ปีที่แล้ว

      i am looking for partner who want to learn bash through co opearation

  • @guilherme5094
    @guilherme5094 2 ปีที่แล้ว

    👍!

  • @jorojo5219
    @jorojo5219 2 ปีที่แล้ว

    Follow!

  • @Alpha_Sadigh
    @Alpha_Sadigh ปีที่แล้ว

    ☯🙏

  • @murad4622
    @murad4622 ปีที่แล้ว

    you can also use
    echo $[100 + 30] # to give you 130