Bash Scripting on Linux (The Complete Guide) Class 04 - Basic Math

  Рет қаралды 66,054

Learn Linux TV

Learn Linux TV

Күн бұрын

Performing math operations in Bash is a bit different than on other platforms, so it may prevent some future confusion if you learn about these things now. And why not? This tutorial is only six minutes long and there's plenty of worse things you can spend your time on, so check out this Bash tutorial and learn how to perform basic math functions!
Brand LPI Linux Essentials Course Available
Check out the new course on Udemy! ➜ learnlinux.link/lpi-course
➜ learnlinux.link/lpi-course
Check out the Linux Shop
In the official shop, you'll find Shirts, hats, stickers, bags and more!
➜ merch.learnlinux.tv
5% discount on LPI exam vouchers
After you finish Jay's new course, get 5% off an LPI exam voucher here:
➜ learnlinux.link/lpi-voucher
Become a Patron
Show your support for Learn Linux TV on Patreon and get access to exclusive perks!
➜ learnlinux.link/patron
Become a Channel Member
Show your support for Learn Linux TV here on KZbin and get access to exclusive perks!
➜ learnlinux.link/member
Mastering Ubuntu Server: 4th Edition
Jay's latest book covers everything you need to know in order to master Ubuntu Server. It's available here:
➜ ubuntuserverbook.com
Linux Gear and Kits
Check out Jay's choice of hardware products, audio/video equipment, and more.
➜ learnlinux.link/amazon
Grab an awesome Pi-powered KVM
Support Learn Linux TV and grab yourself a TinyPilot KVM here:
➜ learnlinux.link/tinypilot
Note: Royalties and/or commission is earned from each of the above links
Individual Sections
00:00 - Intro
01:00 - Math is handled differently in Bash
01:28 - The "evaluate expression" command (expr) in Bash
02:37 - Multiplication in Bash (and how to use an escape symbol)
03:48 - Using variables with math expressions
OTHER BASH SCRIPTING SERIES EPISODES
- Class 01 ➜ Introduction: linux.video/bash1
- Class 02 ➜ Hello World: linux.video/bash2
- Class 03 ➜ Variables: linux.video/bash3
- Class 04 ➜ Math Functions: linux.video/bash4
- Class 05 ➜ If Statements: linux.video/bash5
- Class 06 ➜ Exit Codes: linux.video/bash6
- Class 07 ➜ While Loops: linux.video/bash7
- Class 08 ➜ Universal Update Script: linux.video/bash8
- Class 09 ➜ For Loops: linux.video/bash9
- Class 10 ➜ Where To Store Your Scripts: linux.video/bash10
- Class 11 ➜ Data Streams: linux.video/bash11
- Class 12 ➜ Functions: linux.video/bash12
- Class 13 ➜ Case Statements: linux.video/bash13
- Class 14 ➜ Scheduling Jobs (Part 1): linux.video/bash14
- Class 15 ➜ Scheduling Jobs (Part 2): linux.video/bash15
- Class 16 ➜ Arguments: linux.video/bash16
- Class 17 ➜ Building a Backup Script: linux.video/bash17
- Class 18 ➜ Closing: linux.video/bash18
Full Courses from Learn Linux TV
• Linux Essentials Certification Workshop ➜ learnlinux.link/lpi-course
• Linux Crash Course series ➜ linux.video/cc
• Learn how to use tmux ➜ linux.video/tmux
• Learn how to use vim ➜ linux.video/vim
• Bash Scripting Series ➜ linux.video/bash
• Proxmox VE Cluster Full Course ➜ linux.video/pve
• Learn Ansible ➜ linux.video/ansible
Learn Linux TV on the Web
• Main site ➜ www.learnlinux.tv
• Community ➜ community.learnlinux.tv
• Enterprise Linux Security Podcast ➜ enterpriselinuxsecurity.show
• The Homelab Show Podcast ➜ thehomelab.show
• Content Ethics ➜ www.learnlinux.tv/content-ethics
• Request Assistance ➜ www.learnlinux.tv/request-ass...
Disclaimer
Learn Linux TV provides technical content that will hopefully be helpful to you and teach you something new. However, this content is provided without any warranty (expressed or implied). Learn Linux TV is not responsible for any damages that may arise from any use of this content. The person viewing Learn Linux TV's content is expected to follow their best judgement and to make their best decisions while working with any related technology. Always make sure you have written permission before working with any infrastructure. Also, be sure that you're compliant with all company rules, change control procedures, and local laws.
#linux #baschscripting #linuxforbeginners

Пікірлер: 45
@coolmanabc1231
@coolmanabc1231 11 ай бұрын
I hope your business keeps growing. I appreciate you man.
@helloimatapir
@helloimatapir Жыл бұрын
"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 Жыл бұрын
You literally just helped me fix a script I was writing to convert GPU temps from C to F. Thanks! 👍🏻
@gfjffdgjroedsfdkdslmerrelb4357
@gfjffdgjroedsfdkdslmerrelb4357 Жыл бұрын
i am looking for partner who want to learn bash through co opearation
@VitaDani314
@VitaDani314 9 ай бұрын
If I do it this way, it performs that calculation, but it is also treating it as a command
@imnottellingyoumyname3050
@imnottellingyoumyname3050 5 ай бұрын
@@VitaDani314 same. Got to echo it first.
@marianostone2285
@marianostone2285 4 ай бұрын
thks bro ​@@imnottellingyoumyname3050
@chiomachukwuma2859
@chiomachukwuma2859 5 ай бұрын
I always love your mode of teaching, very simple, clear and straigth forward. Thanks.
@kawa_kxf4509
@kawa_kxf4509 2 ай бұрын
Hello from Athens Greece, I watch your channel and I suggest it to all new people that afraid of using Linux OS.
@patrob3885
@patrob3885 Жыл бұрын
Thank you for your course, it is absolutely amazing that I can understand you the way you teach thank you !
@seta16
@seta16 9 ай бұрын
10/10 good passed short videos and perfect if someone come from any programming languages to make a reference
@CIDC-4500
@CIDC-4500 9 ай бұрын
learned alot from your previous Vid and this as well. Thanks much
@amitkrmandal9974
@amitkrmandal9974 Ай бұрын
Wow Sir Nice . You have Brilliant Knowledge of Linux. I appreciate you Sir . I am Amit From India .
@monjurrahman8688
@monjurrahman8688 11 ай бұрын
I am learning many things from your video. wish your good health
@christianrobert7915
@christianrobert7915 Жыл бұрын
Fun things to try: echo {1..30} echo {30..0}
@OraOraOra
@OraOraOra 6 ай бұрын
thanks🙂
@fabiobianchi3104
@fabiobianchi3104 10 ай бұрын
Great class!!!
@13thravenpurple94
@13thravenpurple94 Жыл бұрын
Great video Thank you
@barkhadibraahim1023
@barkhadibraahim1023 Ай бұрын
thanks jay
@franciscoromogaray3076
@franciscoromogaray3076 5 ай бұрын
classic banger ma bro, short but good
@VideosRichy
@VideosRichy 24 күн бұрын
thank you!!
@leonel_carrizo
@leonel_carrizo Жыл бұрын
Thank you!
@kainaatmakhani6550
@kainaatmakhani6550 Жыл бұрын
Nice lecture
@viesic
@viesic Жыл бұрын
did not know the "expr" command. For mathematical calculations i`m using "bc" command, like that "echo 30 + 10 | bc"
@VitaDani314
@VitaDani314 9 ай бұрын
I noticed that if you surround the * with quotation marks, that works as well
@zakariabasraoui1580
@zakariabasraoui1580 Жыл бұрын
thnx!
@doomtm5500
@doomtm5500 Ай бұрын
!!!!*DOUBT* whenever I create a variable in linux lets say 'myVar' it returns - myVar : command not found..... Can anyone pls tell the solution ??????
@ushousewatch
@ushousewatch 3 ай бұрын
Does bash do order of operations
@knoxduder
@knoxduder 2 ай бұрын
Great question.
@unbekannter_Nutzer
@unbekannter_Nutzer Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
@@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 Жыл бұрын
@@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.
@guilherme5094
@guilherme5094 Жыл бұрын
👍!
@doug2794
@doug2794 3 ай бұрын
These math operations only seem to work with integers.
@jorojo5219
@jorojo5219 Жыл бұрын
Follow!
@jeffschroeder4805
@jeffschroeder4805 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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
@Alpha_Sadigh
@Alpha_Sadigh Жыл бұрын
☯🙏
@murad4622
@murad4622 Жыл бұрын
you can also use echo $[100 + 30] # to give you 130
Bash Scripting on Linux (The Complete Guide) Class 03 - Variables
24:46
Bash Scripting on Linux (The Complete Guide) Class 05 - If Statements
31:43
PINK STEERING STEERING CAR
00:31
Levsob
Рет қаралды 18 МЛН
Why You Should Always Help Others ❤️
00:40
Alan Chikin Chow
Рет қаралды 102 МЛН
Как быстро замутить ЭлектроСамокат
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 13 МЛН
When Jax'S Love For Pomni Is Prevented By Pomni'S Door 😂️
00:26
zsh: Syntax Highlighting, vi-mode, Autocomplete, more
10:39
Luke Smith
Рет қаралды 309 М.
Top 10 Linux Job Interview Questions
16:04
tutoriaLinux
Рет қаралды 2,3 МЛН
BASH scripting will change your life
14:18
NetworkChuck
Рет қаралды 996 М.
Linux for Programmers #7 | Environment Variables
12:31
Akamai Developer
Рет қаралды 34 М.
The Confusion With Linux Shells
12:19
DistroTube
Рет қаралды 12 М.
Become a shell wizard in ~12 mins
12:25
CODE IS EVERYTHING
Рет қаралды 217 М.
Bash Scripting on Linux (The Complete Guide) Class 06 - Exit Codes
27:14
you need to learn BASH Scripting RIGHT NOW!! // EP 1
12:14
NetworkChuck
Рет қаралды 960 М.
PINK STEERING STEERING CAR
00:31
Levsob
Рет қаралды 18 МЛН