Check out a free preview of the full Introduction to Bash, VIM & Regex course
The "Backticks and Arithmetic" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:
James shows how to use backticks, which allow the output of a program in the arguments list of another. Then James illustrates how to do simple arithmetic at the command line.
Transcript from the "Backticks and Arithmetic" Lesson
[00:00:00]
>> James Halliday: Backticks, are something I just covered a little bit ago, but really, really useful. So, there's two ways of doing backticks. You can do them with backticks like this, so if I want to just backticks, basically run a command and whatever was written to standard output, is like kinda just like copy pasted straight into whatever your command was.
[00:00:26]
So if you ever have a command where you kind of need it to be dependent on some other like an argument, to be dependent on some other command that you wanna run, you can use backticks to do that. So the two ways of doing backticks are, you can use just the backtick character like that.
[00:00:44]
These are the keys that's like on my keyboard it's like right below the escape. It's the same one that has the tilda on it. On your keyboard it might be different placed, but you can also use parenthesis like this. So with the dollar sign in front. So these are kind of the two ways.
[00:01:06]
It's a little bit more readable I think sometimes to use the dollar sign, parenthesis case. But, you can mix and match whatever you like. So.
>> James Halliday: A really handy thing if you wanna use the date in your file calculation, you could do something like move well, like some log data you want to write out to file with those the same, or like quote that log.
[00:01:38]
And you can put whatever text you want, like in front of or behind the bit that's gonna be run in backticks. So that makes a file called blah whatever dot log. We can right some more data, so if you had a script that did this, then and it like periodically wrote data to whatever was specified in those backticks, then as soon as the next day rolled around, it would start writing two different files.
[00:02:13]
So this is a really handy was to do some pretty rudimentary but useful logging, for example.
>> James Halliday: Okay, this is a fun thing. So if you want your program to print out whatever the current date is, you could do.
>> James Halliday: There should be an echo in front of that command in the notes.
[00:02:34]
But, like greetings from the year 2017. I can't believe that I first wrote that in 2014, that is such a long time ago [LAUGH] I first gave this work shop. Yeah, so another really fun thing that you can do is not just backticks and running programs, but you can actually do some basic arithmetic as well.
[00:02:58]
So you do this similar to backticks with the form where you have dollar sign parentheses, but you but two parentheses instead of just a single one. And that let's you do arithmetic. LIke if you just run this command with like 4*5+1, you get the result which is 21.
[00:03:18]
So, here I'll go and we'll do some math. 5*11+5. Should be 5 times 12, 60, yep. Another cool thing that you can do, so suppose that I wanna add a thousand to my greeting. So I want it to print greetings from the year 3017. So what I could do, is I could stack some of these ideas, so I'll do the normal substitution with backticks.
[00:03:51]
But I'll add +1000 with some arithmetic operations.
>> James Halliday: Yeah, so I think I'll give everyone a little bit of time to play around with these, cuz they're really fun. And sometimes useful. I don't think that I use the arithmetic expressions too much, but now and then it's just useful to know about that kinda stuff that you can do this kinda thing.
[00:04:22]
I use backticks all the time though. Cuz a lot times you want like a procedurally generated file name, or some procedurally generated options that you get from the output of another command. Related to backticks is another program called Xargs. I don't know if I have anything about Xargs in the notes, but it's certainly worth researching if you haven't used that before.
[00:04:49]
Okay, I can show you an example. So you could write it this way. This is maybe not the best example. But if I go to the previous example, I can show you something cool with xargs. So I could have something that runs like this, or I could do.
[00:05:08]
>> James Halliday: Date + %y xargs echo Greetings from the year block. And this does the same thing.
>> James Halliday: Xarg pretty much takes this, it reads from standard in and copies all of the data from standard in, and into the arguments of the program. So it like, tacks the arguments onto the end of whatever command you specified.
[00:05:38]
So if you wanna read more about the different time strings, if you do mandate. The cool thing is you can use, if you learn this little language for formatting time with the time strings, most programming languages have ways of using the exact same knowledge to format dates in that programming language.
[00:05:59]
There's an NPM module called STRFTIME, that does the same thing. There's also a library, you can read the man page for STRFTIME. There's a C library for doing this kinda stuff. So it comes up again and again.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops