Check out a free preview of the full Complete Intro to Linux and the Command-Line course

The "Subcommands" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that when invoking a command within another command, that command is called a subcommand, and runs a few subcommands as examples

Preview
Close

Transcript from the "Subcommands" Lesson

[00:00:00]
>> Last thing to talk about here. Let's talk about sub commands really quick. Let's say, I wanna know, I have the string that I wanna output. Let's just do what I have right there. I can say echo, I think and then I wanna run who am I in the middle of here.

[00:00:20]
So if I put dollar sign, and then I put parentheses here, and I put who am I right there, is a really cool user. It's actually going to go in and it's going to run a sub Command here. So it's actually going to execute a separate command here and run whatever between the parentheses, whatever that returns, whatever that returns to standard out.

[00:00:45]
It's going to put that there, or whatever we would have piped right? That makes sense. So this is a subcommand. So I can do echo the current date is $(date), right? So that's really useful, it's useful for doing something in a subcommand, getting all that information, and returning it back up to the higher process basically.

[00:01:15]
So let's say you had a job or like a some sort of task that's running every day that just goes in and checks the date and it checks the uptime and attaches that to a file. The way you could do that is echo Date and then you can do plus percent x and that'll just give you the date.

[00:01:34]
It won't give you like the rest of the time. I just found that from the man from the manual. And then I can do a second sub command here for the uptime. And I can run that and put that into log.txt like that. And now if I look at log.txt, you can see here, here's the date.

[00:01:56]
Here's the dash that we put right there, right? And here's the uptime, right? And I can run that a bunch of times. And if I look at log.txt, you can see here, this was what your lot like each line would be like one entry in your log or something like that.

[00:02:20]
So how often do I use sub commands, once every 3 months like I don't use them that often but the times that they end up coming in useful, like, I'm very happy I know what they are. Yeah, anything inside of that dollar sign, parentheses there, it can be anything.

[00:02:39]
It could be echo. You could be. Did not like that. Yeah, cuz it was trying to anyway, trying to tab completion was not something where you wanna be doing that cat log.txt, right? So I was able to read in the file to standard in and then cat that.

[00:03:09]
And then have that return from the subprocess and then have that return here. So that's why it says hi and then user at the end, and then, all of log.txt in there. One last thing here, you will see another older way of doing this and say hi, and then you can put backticks.

[00:03:37]
And you'll notice that that actually ends up working as well. This is the old way of doing it. In fact, I was reading that it's like very old, like it hasn't been, like the primary way of doing that for many, many years. But they still include it for backwards compatibility purposes.Always just do the dollar sign parentheses because it's it's a superior way of doing that.

[00:04:03]
For one, you can nest them. So you can actually have multiple sub commands nested, whereas with backticks, that wouldn't, that does not work. And it's less ambiguous. It's easier to read. It's just a better way of doing it and there's more options of what you can do. But I include the backticks just so that so, if you read in like a Stack Overflow answer backticks also will execute something as a sub command.

[00:04:32]
>> Can you discuss the other columns of ps aux.
>> Maybe, I ignore most of them so alright so user that will make sense right? PID, CPU percentage usage, right. So you can, [COUGH] I think you can actually sort this, and so you can see if you have something that's pegging your CPU, you can figure out what that is and you can kill it.

[00:05:02]
Same with percentage memory. I do not know what VSZ, I don't know what any of these are, TTY. STAT, START, obviously START is when you start it, TIME is how long it's been running, and the COMMAND is obviously what you're running. But for these ones in the middle, I would just have to go look up what they are.

[00:05:22]
But suffice to say they're not important or I haven't had to know them in my entire career. So they're not important for me to know. If I don't know what it's not important, but I'm just gonna I just I've never had to look it up.
>> Speaking of environments Pass and putting them in your profile.

[00:05:45]
How do you know which file your computer is actually running, if it's a profile or a batch profile or bash RC?
>> That's a good question. Your login shell like whenever you log into a new session of bash, it will always read bash RC 100% of the time.

[00:06:07]
I believe it should read read bash RC bash profile will only run on the First interactive shell that's invoked that day. And then different shells have different files names, but they usually kind of follow the same thing like ZSH is ZSHRC, Right. I don't think they have a ZSH profile.

[00:06:28]
I think they just have ZSH. RRC same with fish cell it's dot fish RC. So,just stick to the dot name of the shell RC file in your home directory and you'll be golden

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now