The Bash 'test' Command Tutorial

preview_player
Показать описание
Bash has a builtin called "test" that can check file types and compare values of variables. Essentially, "test" will check if an expression is true or false. This is incredibly useful in Bash scripting!

REFERENCED:

WANT TO SUPPORT THE CHANNEL?

DONATE CRYPTO:
💰 Bitcoin: 1Mp6ebz5bNcjNFW7XWHVht36SkiLoxPKoX
🐶 Dogecoin: D5fpRD1JRoBFPDXSBocRTp8W9uKzfwLFAu
📕 LBC: bMfA2c3zmcLxPCpyPcrykLvMhZ7A5mQuhJ

DT ON THE WEB:

FREE AND OPEN SOURCE SOFTWARE THAT I USE:

Your support is very much appreciated. Thanks, guys!
Рекомендации по теме
Комментарии
Автор

In modern shells, [[ & [ are built in but in the Borne Shell [ was a symbolic link to the "test" program. By the way, great video! Not many YouTubers have the depth of knowledge to understand this.

esra_erimez
Автор

A couple of points:

6:50 Inside double quotes, the only metacharacters that retain their special meaning for bash are $ ` \ an if histexapnd is enabled also !. The tilde is treated like a regular character.

The ternary operator only exists in bash for arithmetic expression, as in a=1 b=2; c="$((a>b?a:b))", c will be 2. Doing cmd1 && cmd2 || cmd3, might work most of the time but can also be a disaster if you do something like [[ -f $some_file ]] && cmd2 || rm -rf *, because you want to test if a file exists and then run a command and nothing more, if the file does not exist you want to delete the current working directory, but if for some reason cmd2 fails, then even if $some_file exists your folder will get deleted, your life as a programmer will end, and you will be forced to prostitute yourself in a gay bar in Somalia.

valentin-catalin
Автор

It's interesting that in all programming 0 means false and non 0 means true, but in the exit codes it's the opposite, and bash knows to do that, to treat a 0 exit code a true value.

Neat showing. Basically you'll never use it directly in the CLI (other than maybe testing if you wrote it correctly), but it's one of the foundations for effective, useful scripting. Basically all install scripts use this to check files, directories and permissions, to make sure they're doing the things correctly.

Winnetou
Автор

man test, is a goto for quickly remembering the difference between -n -z etc. to use in if statements

milohoffman
Автор

I have to say, Qtile just gives such a refreshing and pleasant feel to the eyes, i have tried so hard and wasted tons of hours customizing and trying all the others, but no other WM checks every box that i need and it has everything, a bar, run launcher, wallpaper picker, widgets, scratchpads, lots of layouts, keychords, easy config and easy to setup.

besnikrrustemi
Автор

echo $? To see the return value of the previous command.

raidensama
Автор

thanks for the vid and definitely thanks for the man page on test because that's a nice concise list to keep handy

MunnyLerner
Автор

7:19 another thing about that: if you didn't use double quotes and just typed in the path like that, it *would* be fine with ~
(at least on fish)

Axlefublr
Автор

A single [ is a command (usually a builtin), but a double [[ is actually shell syntax. This actually means than [[ is a little quicker to run, on shells that support it.

Of course the shells that don't support it likely have quicker startup times than bash, so there's a case for sticking to the old way too. But if you're using [ in your .bashrc, [[ is a free upgrade ;)

killistan
Автор

Hey DT can you talk about docker and docker containers? I know it's mostly for servers but I my opinion, there are some usecases for desktop users as well.
Thank you❤

master
Автор

Note that test can to execute any bash command, not only echo. f.e.: test $browser=1 && find / -name firefox || find / -name chomium I like very much that a linuxtuber teaches Bash commands. Thank you.

Ferran-Gnu-Linux
Автор

You can also just do this to see if a variable is empty
[[ $emptystring ]] && echo "True" || echo "False"

You don't need the -n ro -z. I use this a lot when getting user input. If a user don't enter something that is needed I will exit the script.

read -p "Enter Your Name: " name
[[ $name ]] || exit 1

So if the user doesn't enter a name, the script will exit. If they do, then the script continues.

DigitalMetal
Автор

I always forget that test exists and when I need to look up the options I've always searched in the bash or zsh documentation since there's no man page for [[, and i forget frequently enough that I had a cheat sheet saved with all the available options having not realized that test has a man page that I could have been using all this time.

notimportant
Автор

I use test for if files exists in aliases

SB-qmwg
Автор

How to do that lovely font, color, and time stamp ❤

wizardatmath
Автор

What's the color scheme in thumbnail? Looking cool.

ShrekBytes
Автор

posted 6 hours ago... can I be the first to comment on the cursor movement? or rather, the *lack* of cursor movement shortcuts 😆 future episode, "Using ctl-A to move cursor..." 😁😁

SFDestiny
Автор

Hlo sir, please make socket programming video by using bash❤

GajendraMahat
Автор

....

BASH HAD A UNIT TESTING UTILITY???

uuu
Автор

aware /usr/bin/test and /usr/bin/[, posix complaint just better

newplayer