Linux Commands for Beginners 18 - Variables — Transcript
Full transcript
- 0:00[Music]
- 0:20hey guys welcome to another video in my
- 0:22Linux commands for beginner series and
- 0:25this time around we're gonna be taking a
- 0:28look at variables probably something I
- 0:31should have gone over earlier but it's
- 0:33an easy concept I think maybe it's
- 0:34pretty cool to have a little break and
- 0:36just do something easy for a minute
- 0:38before we get into other items on the
- 0:40series but what I want to show you guys
- 0:43how to do is how to create a variable
- 0:45and read from it it's a very important
- 0:48thing it's more important for scripting
- 0:50and programming but it is something
- 0:51you're gonna run into when you're
- 0:53running commands in Linux so let's go
- 0:55ahead and take a look back here on my
- 0:58terminal I'll just give you a quick
- 0:59refresher we have the echo command which
- 1:02I mentioned in a previous video anything
- 1:06you type into echo and the quotes there
- 1:08is going to be echoed back at you but
- 1:12the echo command is useful in regards to
- 1:14seeing what variables contain so what
- 1:18we're gonna do right now is create a
- 1:19variable and then we'll use the echo
- 1:21command to read that back so I'm going
- 1:25to create a variable called hello msg so
- 1:30hello message and it's very common to
- 1:32put variable names and caps but you
- 1:34don't actually have to do that now if no
- 1:36space I'm going to do an equal sign and
- 1:38then in double quotes I'm going to put
- 1:41hello world so basically I'm creating a
- 1:44variable called Hello message
- 1:46abbreviated equal sign so I want to set
- 1:50it equal to a string hello world and
- 1:52I'll press Enter
- 1:54okay so nothing happened well actually
- 1:56something did happen but you're just not
- 1:58seeing the output now we do have a new
- 2:01variable so I'll go ahead and clear the
- 2:03screen and what I'm actually going to do
- 2:06is read that variable back so to do so I
- 2:09will use echo and Hello msg however
- 2:13that's not gonna work
- 2:14it's just echoing hello msg it's just
- 2:18you know the same if I say echo hello or
- 2:21echo random it's going to echo back
- 2:24anything I put there but I want the
- 2:27actual variable I want to see what is
- 2:29inside
- 2:30hello msg so when you refer to a
- 2:34variable on bosch which is basically
- 2:36what the shell is here you have to put a
- 2:39dollar sign in front of it that
- 2:41basically tells the command-line
- 2:43interpreter that you want to refer to
- 2:47the contents of a variable when you
- 2:49declare the variable we don't have the
- 2:51dollar sign but we do need it when we
- 2:53want to refer back to it and you can see
- 2:56there that I get hello world which is
- 2:59what we would expect to get now when
- 3:02again when I saved the variable I didn't
- 3:04have a dollar sign in front of it but to
- 3:06refer back to it I will need to do that
- 3:08and I could also echo the dollar sign
- 3:12question mark get a return code return
- 3:14code is zero which means that it was
- 3:16successful it read that variable back so
- 3:19moving right along let's create another
- 3:21variable will do my underscore num will
- 3:25set it to equal three and then I'll do
- 3:29my num - I'll set that equal to ten so
- 3:32just like any other variable I can echo
- 3:34my num the equals three and I could do
- 3:40the same thing with the other video
- 3:41variable that equals ten
- 3:44now one thing I do want to mention
- 3:45though is that these variables are
- 3:48specific to my sessions so if I open a
- 3:51new terminal
- 3:52I do echo my num nothing and the reason
- 3:59why is because the variable exists in
- 4:01this session doesn't exist here so if I
- 4:03close a terminal I am going to lose
- 4:05those items now we went over bash RC
- 4:08briefly in a previous video so I could
- 4:10put some variables in there if I wanted
- 4:12to retain them I just wanted to mention
- 4:14that real quick but anyway getting back
- 4:16to the topic at hand I created two
- 4:19variables my num and mind um two that
- 4:22are equal to three in ten respective so
- 4:24perhaps a more useful example I could do
- 4:26my underscore name equals and quotes put
- 4:31my name there I have that variable so of
- 4:34course I can't echo that back
- 4:36in case I forgot my name in which case
- 4:39I'm just reminded of what that was so I
- 4:41have a name what can I actually do with
- 4:43that so what I'm going to do is try an
- 4:47echo command I'll do echo and then my
- 4:51name is my name then I'll end the quotes
- 4:57let's see what happens
- 4:59says my name is J so what I was able to
- 5:03do is refer to this variable inside
- 5:07another command so to see a more
- 5:09practical example of that do my derp
- 5:12equals and quotes slash at sea there we
- 5:17go press Enter
- 5:18and just to make sure it saved it right
- 5:23it did so I could do now LS my derp now
- 5:29we see the contents of the Etsy
- 5:32directory are printed on the screen now
- 5:34obviously typing my Durr is more
- 5:37characters than just simply typing /e TC
- 5:40but if you had a very long path it might
- 5:44make more sense to actually have a
- 5:47variable as a directory so that just
- 5:49goes to show you that you can save
- 5:50variables and then refer to them in
- 5:52commands so that way you don't have to
- 5:55re-type something over and over again if
- 5:58it's a long path or something like that
- 6:01then you can go ahead and save it in a
- 6:02variable to save typing and this
- 6:05especially helps when you have scripts
- 6:08we're not going to get into bash
- 6:09scripting much in this video if at all I
- 6:12just wanted you to know that that's
- 6:13generally where you're going to find
- 6:15this quite often you're gonna have
- 6:16variables and that's going to basically
- 6:19allow you to save some kind of
- 6:21information so that you can refer to
- 6:23that later and what's fun is to see what
- 6:26variables are in your sessions so if I
- 6:29was to do env and press enter we can
- 6:33actually see a bunch of variables now
- 6:34these will need the dollar sign if you
- 6:37wanted to refer to them so for example
- 6:39we see I have a variable user it's equal
- 6:41to me so I could do echo
- 6:43user but that's not going to work I need
- 6:45to put the dollar symbol in there that
- 6:48just goes to show you that we still need
- 6:50the dollar symbol it's not always going
- 6:52to be there but basically this env shows
- 6:54my environment in my logged in session
- 6:58and all the variables that are included
- 7:01in that session or in this current
- 7:03session and there's actually quite a few
- 7:06so different programs might use this
- 7:07differently for example the display
- 7:10variable is one that I'll often use that
- 7:12refers to the monitor that applications
- 7:15will show up on by default you can
- 7:18actually and this beyond the scope of
- 7:20the series forward your display to
- 7:22another machine and I can launch a
- 7:25program on this computer but actually
- 7:27have the program show up on a completely
- 7:29different computer basically it's
- 7:31referring to X forwarding again outside
- 7:34the scope of this video but just to show
- 7:36you that this variable is important and
- 7:38probably something you'll end up using
- 7:40at some point or another so go ahead and
- 7:42clear the screen so I'm going to create
- 7:44a new variable and I'm just going to use
- 7:46lower case because I'm lazy and I'm
- 7:49going to make it equal to Linux simple
- 7:57so let's do a MV let's see if we see
- 8:02that variable it's lower case so you
- 8:04know what it really should stand out
- 8:05because everything here begins with
- 8:07upper case but if I scroll through here
- 8:09well I'm not seeing it so I'll go ahead
- 8:13and clear the screen so what's going on
- 8:14here why if I have this variable and
- 8:16it's recognized I can even use the echo
- 8:18command against it why doesn't it show
- 8:20up in the environment well it's not
- 8:22actually part of the environment unless
- 8:24I make it part of the environment which
- 8:26means it can exist beyond my shell so
- 8:29again I created this variable right here
- 8:31which we know it does indeed work it's
- 8:33not an illusion it gets a return value
- 8:35of 0 so we know that it works but it's
- 8:37not in our environment if I was to open
- 8:39a new shell to open up a new terminal
- 8:42window here and if I do the same thing
- 8:44echo my VAR it doesn't work so let's try
- 8:48this a bit differently I'm gonna do
- 8:49export and then I'm going to type my VAR
- 8:54e
- 8:55Linux so I'm just basically declaring a
- 8:59variable called my var setting equal to
- 9:00Linux by putting X port in front what's
- 9:02gonna happen so is anything different
- 9:05here so let's see when echo my var seems
- 9:08to work fine
- 9:11echo code seems to return I'm gonna
- 9:14clear the screen so now let's check out
- 9:16the environment again I scroll through
- 9:19and see if our variable is listed and
- 9:21immediately I see it is listed here
- 9:24whereas it was not actually listed
- 9:26before we actually made this variable
- 9:29part of our environment so that was a
- 9:32relatively easy video I just showed you
- 9:33guys how to set variables and read them
- 9:35back I also showed you that you can use
- 9:38those variables in Linux commands now
- 9:40this is a concept that's gonna go you
- 9:43know above and beyond as the you know as
- 9:45your knowledge grows and you learn more
- 9:47things so yeah it's a very simple
- 9:48example but the possibilities you can do
- 9:51with variables I think you're gonna find
- 9:54that you can extend that quite a bit I
- 9:56mean you can for example you could have
- 9:58a script that takes arguments maybe have
- 10:01a directory you give as an argument it
- 10:03takes that argument as standard input
- 10:04and then it refers to it in the program
- 10:08maybe 50 times but you only have to
- 10:10refer to it that one time is standard
- 10:12input and since it's a variable you know
- 10:14you don't have the hard code at that
- 10:15much time I mean you shouldn't hard code
- 10:17anything anyway but it just goes to show
- 10:19you variables are something that are
- 10:20going to be you know a mainstay in your
- 10:23tool set especially as your knowledge
- 10:25grows so I hope that was helpful for you
- 10:27guys just you know make sure you
- 10:29understand setting and reading back your
- 10:31variables just you know give it a shot
- 10:33play around with it and then once you're
- 10:35all caught up I will see you in the next
- 10:37video thank you so much for watching my
- 10:40video I really appreciate it if you want
- 10:42to help me out make sure you check out
- 10:44the description below this video where
- 10:46you'll find links to my latest book
- 10:48mastering a boon to server second
- 10:50edition as well as my patreon page if
- 10:52you like this video be sure to click
- 10:53that like button and share it on Twitter
- 10:56or any other social media network and be
- 10:58sure to subscribe so you'll be the first
- 11:00to see my latest videos as they're
- 11:02uploaded thanks again
About this transcript
This page contains the full transcript of Linux Commands for Beginners 18 - Variables by Learn Linux TV, generated from the public captions YouTube serves with the video. The transcript has 1,920 words across 258 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.