YouTube2Text

Linux Commands for Beginners 18 - Variables — Transcript

by Learn Linux TV · 1,920 words · 258 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:20hey guys welcome to another video in my
  3. 0:22Linux commands for beginner series and
  4. 0:25this time around we're gonna be taking a
  5. 0:28look at variables probably something I
  6. 0:31should have gone over earlier but it's
  7. 0:33an easy concept I think maybe it's
  8. 0:34pretty cool to have a little break and
  9. 0:36just do something easy for a minute
  10. 0:38before we get into other items on the
  11. 0:40series but what I want to show you guys
  12. 0:43how to do is how to create a variable
  13. 0:45and read from it it's a very important
  14. 0:48thing it's more important for scripting
  15. 0:50and programming but it is something
  16. 0:51you're gonna run into when you're
  17. 0:53running commands in Linux so let's go
  18. 0:55ahead and take a look back here on my
  19. 0:58terminal I'll just give you a quick
  20. 0:59refresher we have the echo command which
  21. 1:02I mentioned in a previous video anything
  22. 1:06you type into echo and the quotes there
  23. 1:08is going to be echoed back at you but
  24. 1:12the echo command is useful in regards to
  25. 1:14seeing what variables contain so what
  26. 1:18we're gonna do right now is create a
  27. 1:19variable and then we'll use the echo
  28. 1:21command to read that back so I'm going
  29. 1:25to create a variable called hello msg so
  30. 1:30hello message and it's very common to
  31. 1:32put variable names and caps but you
  32. 1:34don't actually have to do that now if no
  33. 1:36space I'm going to do an equal sign and
  34. 1:38then in double quotes I'm going to put
  35. 1:41hello world so basically I'm creating a
  36. 1:44variable called Hello message
  37. 1:46abbreviated equal sign so I want to set
  38. 1:50it equal to a string hello world and
  39. 1:52I'll press Enter
  40. 1:54okay so nothing happened well actually
  41. 1:56something did happen but you're just not
  42. 1:58seeing the output now we do have a new
  43. 2:01variable so I'll go ahead and clear the
  44. 2:03screen and what I'm actually going to do
  45. 2:06is read that variable back so to do so I
  46. 2:09will use echo and Hello msg however
  47. 2:13that's not gonna work
  48. 2:14it's just echoing hello msg it's just
  49. 2:18you know the same if I say echo hello or
  50. 2:21echo random it's going to echo back
  51. 2:24anything I put there but I want the
  52. 2:27actual variable I want to see what is
  53. 2:29inside
  54. 2:30hello msg so when you refer to a
  55. 2:34variable on bosch which is basically
  56. 2:36what the shell is here you have to put a
  57. 2:39dollar sign in front of it that
  58. 2:41basically tells the command-line
  59. 2:43interpreter that you want to refer to
  60. 2:47the contents of a variable when you
  61. 2:49declare the variable we don't have the
  62. 2:51dollar sign but we do need it when we
  63. 2:53want to refer back to it and you can see
  64. 2:56there that I get hello world which is
  65. 2:59what we would expect to get now when
  66. 3:02again when I saved the variable I didn't
  67. 3:04have a dollar sign in front of it but to
  68. 3:06refer back to it I will need to do that
  69. 3:08and I could also echo the dollar sign
  70. 3:12question mark get a return code return
  71. 3:14code is zero which means that it was
  72. 3:16successful it read that variable back so
  73. 3:19moving right along let's create another
  74. 3:21variable will do my underscore num will
  75. 3:25set it to equal three and then I'll do
  76. 3:29my num - I'll set that equal to ten so
  77. 3:32just like any other variable I can echo
  78. 3:34my num the equals three and I could do
  79. 3:40the same thing with the other video
  80. 3:41variable that equals ten
  81. 3:44now one thing I do want to mention
  82. 3:45though is that these variables are
  83. 3:48specific to my sessions so if I open a
  84. 3:51new terminal
  85. 3:52I do echo my num nothing and the reason
  86. 3:59why is because the variable exists in
  87. 4:01this session doesn't exist here so if I
  88. 4:03close a terminal I am going to lose
  89. 4:05those items now we went over bash RC
  90. 4:08briefly in a previous video so I could
  91. 4:10put some variables in there if I wanted
  92. 4:12to retain them I just wanted to mention
  93. 4:14that real quick but anyway getting back
  94. 4:16to the topic at hand I created two
  95. 4:19variables my num and mind um two that
  96. 4:22are equal to three in ten respective so
  97. 4:24perhaps a more useful example I could do
  98. 4:26my underscore name equals and quotes put
  99. 4:31my name there I have that variable so of
  100. 4:34course I can't echo that back
  101. 4:36in case I forgot my name in which case
  102. 4:39I'm just reminded of what that was so I
  103. 4:41have a name what can I actually do with
  104. 4:43that so what I'm going to do is try an
  105. 4:47echo command I'll do echo and then my
  106. 4:51name is my name then I'll end the quotes
  107. 4:57let's see what happens
  108. 4:59says my name is J so what I was able to
  109. 5:03do is refer to this variable inside
  110. 5:07another command so to see a more
  111. 5:09practical example of that do my derp
  112. 5:12equals and quotes slash at sea there we
  113. 5:17go press Enter
  114. 5:18and just to make sure it saved it right
  115. 5:23it did so I could do now LS my derp now
  116. 5:29we see the contents of the Etsy
  117. 5:32directory are printed on the screen now
  118. 5:34obviously typing my Durr is more
  119. 5:37characters than just simply typing /e TC
  120. 5:40but if you had a very long path it might
  121. 5:44make more sense to actually have a
  122. 5:47variable as a directory so that just
  123. 5:49goes to show you that you can save
  124. 5:50variables and then refer to them in
  125. 5:52commands so that way you don't have to
  126. 5:55re-type something over and over again if
  127. 5:58it's a long path or something like that
  128. 6:01then you can go ahead and save it in a
  129. 6:02variable to save typing and this
  130. 6:05especially helps when you have scripts
  131. 6:08we're not going to get into bash
  132. 6:09scripting much in this video if at all I
  133. 6:12just wanted you to know that that's
  134. 6:13generally where you're going to find
  135. 6:15this quite often you're gonna have
  136. 6:16variables and that's going to basically
  137. 6:19allow you to save some kind of
  138. 6:21information so that you can refer to
  139. 6:23that later and what's fun is to see what
  140. 6:26variables are in your sessions so if I
  141. 6:29was to do env and press enter we can
  142. 6:33actually see a bunch of variables now
  143. 6:34these will need the dollar sign if you
  144. 6:37wanted to refer to them so for example
  145. 6:39we see I have a variable user it's equal
  146. 6:41to me so I could do echo
  147. 6:43user but that's not going to work I need
  148. 6:45to put the dollar symbol in there that
  149. 6:48just goes to show you that we still need
  150. 6:50the dollar symbol it's not always going
  151. 6:52to be there but basically this env shows
  152. 6:54my environment in my logged in session
  153. 6:58and all the variables that are included
  154. 7:01in that session or in this current
  155. 7:03session and there's actually quite a few
  156. 7:06so different programs might use this
  157. 7:07differently for example the display
  158. 7:10variable is one that I'll often use that
  159. 7:12refers to the monitor that applications
  160. 7:15will show up on by default you can
  161. 7:18actually and this beyond the scope of
  162. 7:20the series forward your display to
  163. 7:22another machine and I can launch a
  164. 7:25program on this computer but actually
  165. 7:27have the program show up on a completely
  166. 7:29different computer basically it's
  167. 7:31referring to X forwarding again outside
  168. 7:34the scope of this video but just to show
  169. 7:36you that this variable is important and
  170. 7:38probably something you'll end up using
  171. 7:40at some point or another so go ahead and
  172. 7:42clear the screen so I'm going to create
  173. 7:44a new variable and I'm just going to use
  174. 7:46lower case because I'm lazy and I'm
  175. 7:49going to make it equal to Linux simple
  176. 7:57so let's do a MV let's see if we see
  177. 8:02that variable it's lower case so you
  178. 8:04know what it really should stand out
  179. 8:05because everything here begins with
  180. 8:07upper case but if I scroll through here
  181. 8:09well I'm not seeing it so I'll go ahead
  182. 8:13and clear the screen so what's going on
  183. 8:14here why if I have this variable and
  184. 8:16it's recognized I can even use the echo
  185. 8:18command against it why doesn't it show
  186. 8:20up in the environment well it's not
  187. 8:22actually part of the environment unless
  188. 8:24I make it part of the environment which
  189. 8:26means it can exist beyond my shell so
  190. 8:29again I created this variable right here
  191. 8:31which we know it does indeed work it's
  192. 8:33not an illusion it gets a return value
  193. 8:35of 0 so we know that it works but it's
  194. 8:37not in our environment if I was to open
  195. 8:39a new shell to open up a new terminal
  196. 8:42window here and if I do the same thing
  197. 8:44echo my VAR it doesn't work so let's try
  198. 8:48this a bit differently I'm gonna do
  199. 8:49export and then I'm going to type my VAR
  200. 8:54e
  201. 8:55Linux so I'm just basically declaring a
  202. 8:59variable called my var setting equal to
  203. 9:00Linux by putting X port in front what's
  204. 9:02gonna happen so is anything different
  205. 9:05here so let's see when echo my var seems
  206. 9:08to work fine
  207. 9:11echo code seems to return I'm gonna
  208. 9:14clear the screen so now let's check out
  209. 9:16the environment again I scroll through
  210. 9:19and see if our variable is listed and
  211. 9:21immediately I see it is listed here
  212. 9:24whereas it was not actually listed
  213. 9:26before we actually made this variable
  214. 9:29part of our environment so that was a
  215. 9:32relatively easy video I just showed you
  216. 9:33guys how to set variables and read them
  217. 9:35back I also showed you that you can use
  218. 9:38those variables in Linux commands now
  219. 9:40this is a concept that's gonna go you
  220. 9:43know above and beyond as the you know as
  221. 9:45your knowledge grows and you learn more
  222. 9:47things so yeah it's a very simple
  223. 9:48example but the possibilities you can do
  224. 9:51with variables I think you're gonna find
  225. 9:54that you can extend that quite a bit I
  226. 9:56mean you can for example you could have
  227. 9:58a script that takes arguments maybe have
  228. 10:01a directory you give as an argument it
  229. 10:03takes that argument as standard input
  230. 10:04and then it refers to it in the program
  231. 10:08maybe 50 times but you only have to
  232. 10:10refer to it that one time is standard
  233. 10:12input and since it's a variable you know
  234. 10:14you don't have the hard code at that
  235. 10:15much time I mean you shouldn't hard code
  236. 10:17anything anyway but it just goes to show
  237. 10:19you variables are something that are
  238. 10:20going to be you know a mainstay in your
  239. 10:23tool set especially as your knowledge
  240. 10:25grows so I hope that was helpful for you
  241. 10:27guys just you know make sure you
  242. 10:29understand setting and reading back your
  243. 10:31variables just you know give it a shot
  244. 10:33play around with it and then once you're
  245. 10:35all caught up I will see you in the next
  246. 10:37video thank you so much for watching my
  247. 10:40video I really appreciate it if you want
  248. 10:42to help me out make sure you check out
  249. 10:44the description below this video where
  250. 10:46you'll find links to my latest book
  251. 10:48mastering a boon to server second
  252. 10:50edition as well as my patreon page if
  253. 10:52you like this video be sure to click
  254. 10:53that like button and share it on Twitter
  255. 10:56or any other social media network and be
  256. 10:58sure to subscribe so you'll be the first
  257. 11:00to see my latest videos as they're
  258. 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.