Tips and Secrets to Shell Efficiency
- January 3, 2024
- Posted by: MainInstructor
- Category: C Go Regular Expressions SQL
Video Title: Tips and Secrets to Shell Efficiency
My admittedly short yet extended period of programming i have been hard-pressed to find a programmer who doesn’t prefer to work at least as much as i can in the command line and i think there’s a few endemic reasons behind this for instance programmers prefer to work with the
Keyboard given that we do so much typing introducing another input device seems just silly and inefficient at least that’s my opinion and the opinion of many others there’s obviously things that go against this but i think that there is a certain aspect to the shell that lots of people have discovered yet certain
Certain people haven’t that inch introduces so much more efficiency into your work because you are getting the machine to work for you rather than you working for the machines sounds like a rage against the machine talking point but it is true we uh invented computers effectively out of
Laziness so that they could do things for us and the shell is a perfect example of how we have managed to design something where we get the machines to do work for us and so this is basically me on the behalf of lots of people who have discovered these features already
Kind of throwing down a ladder for the people who are still stuck at the bottom of the tree trying to get up who haven’t yet discovered some of the more intriguing features of the shell and some of these are so insignificant it might seem like it would never be
Useful but then once you start using it properly then you’ll start to realize that uh these these are some of the most life-saving features in the world so the first thing i’d like to talk about is the humble cd command so given no arguments cd will cd you into your home directory
Very simple cd also provides you access to two directories or actually there are two directories in every directory which are actually logical they’re not real but they can be used as though they were real directories and these are dot dot and dot the dot directory is the current
Directory the dot dot directory if we ls dot dot we can see the users on the system if we ls dot we can see the files in my home folder the dot folder is useful when you need to provide a local path for instance if
I compile the test.c binary uh if i run if i just type a dot out you can see that it can’t find it because it’s not in path but if we run dot slash a dot out we can see that it runs in the same way
That if i go into the home folder and run ethern slash a dot out we get the same result as the dot is simply being substituted for the working directory the dot dot directory is incredibly useful as it allows you to traverse backwards up the directory tree but i
See it being used inefficiently very often for instance to get to the root i see people often go cd dot ls cd dot dot ls people forget that because dot dot is simply just a logical directory inside every directory you can simply do cd dot slash dot dot
It can be used just like any other directory in the tree for instance if i go into hobbit and then ui components i’m working on i don’t know the list component discovered a bug in the scrolling code yet again and then i need to test it at the root well rather than
Going cd dot dot cd dot dot to get to the root and then running make instead i could simply run cd dot dot slash dot dot i’m going to show an example now for instance say i need to get back to a directory which is in the project
Directory or even in the programming directory let’s say i need to get to a directory that’s in the programming test c directory well instead of going cd dot dot cd and then you can write a semicolon we’ll come into that in a minute cd dot
Dot then cd dot dot all in a row and then finally cding into the correct directory instead you can instead simply write cd dot dot slash dot dot slash tests slash c slash uh low level or is it linking let’s do perf and then we have specified a path which
Will be implicitly expanded to where we want to go you don’t have to only use one dot dot at a time this is a perfect example as well to show off the second most useful feature in my opinion of the cd binary and that is the cd dash
We can see that when i run cd dash it printed out a path and moved me into this path let’s see what happens if i type it again it’s moving back to this directory again and it’s printed it again so if you run this multiple times you’ll
Notice that what it’s doing is it’s swapping you between these two directories if i cd to my home folder by just typing cd and then i cd dash you can see that it’s going to swap us between these two directories if we actually read the man page of cd
We can see that a special case is given for cd with just dash and uh if we can find the relevant section somewhere uh this shall be equivalent to the command cd and print working directory it changes to the previous working directory and writes its name so what
This can be used for is to quickly switch between two directories that you’re doing work in for instance if i’m working on test.c i don’t know maybe we want to indent these with a tab go back to profit look in main.go you can see that i just switch between two
Far like far vastly removed directories in my file system tree just by using uh cd dash because it seems as though the designers of the shell realized that very often we like to interchange between two directories very quickly or we like to temporarily change to another directory
For instance say that i’m going into dsjs cli and i’m going to go into source and then conf and then i’m going to edit the conf dot c source file this here relies on a utility function called path concat which will perform a static concatenation into this buffer here mfn
So i could use gd and vim to go to this definition or what if i need to edit the source file around it well let’s act for path concat and let’s give her ack the path dot dot so that it can do the whole source and we’re going to see that path
Can cat is defined in file io dot c so let’s go into dot slash slash util and let’s vim fido.c and then let’s look for um half concat okay we can now find our bug i don’t know uh i don’t know fixbug here and then we can just cd dash to get back
To what we were working on in here because what this can be used for is to temporarily change path to something else that we are going to use just once i’m going to type cd with no arguments again to go to my home directory and i want to talk a little bit about
Shell globbing because shell globbing is probably one of the most underused and yet underrated features that the shell provides and i should note as well that most of the things we talked about so far are posix compliant so even on a shell like dash you can still do this
You can see that cd dash is still working perfectly fine it feels like a bashism but it’s not because the posix designers are slightly smarter than it seems when they design sockets so uh shell globbing allows us to match any path name which matches a certain pattern and a very common mistake that
People make is that they think that these patterns are regular expression patterns they are not in fact if we look at the man page for shell globs they explain that it is not the same as a regular expression they say it’s somewhere i can’t remember it’s no regular
Expressions wild card patterns are not regular expressions shell globbing is far removed from regular expressions in that there are some functional changes for instance this question mark is functionally equivalent to the regular expression dot character and the star is equivalent to a regular expression dot star character and these brackets or character groups
Are equivalent to extended irregular expressions but without having to know what all of that means let’s take a look at some examples so when you type a star you’re basically seeing that any list of characters to go here so if i type ls this is the same as
Typing ls star apart from the fact that all of the things in that list that match it will be given as arguments so if i type ls star and then use a bind to auto complete it we can see that it’s filled in every single directory in the folder
Let’s say that i want to get all audio files star dot mp uh star dot mp3 well you can see it’s matched the doom soundtrack in here that i can now play and uh let’s say i want to match all source files for instance let’s go into programming projects
Cli source and say file star dot c we can see it’s detected the two only c source files but let’s say i want to match all c files and all header files we can see it’s now found us all c files and all header files as well and detected
They’re all c source files for the file command and the other type i’m going to use cd dash again just to prove the point the other type of character that you can use in these glob patterns apart from character classes which will match any of the characters within them and the
Star which matches anything at all is the question mark and this says that you can match any single character here so for instance i’m going to um if i make some files i’m going to make the files 1 2 3 4 dot c and this is just for an example
And i’m going to talk about those bracket lists in a minute but when i’ve created these files each one of these has a certain beginning but let’s say as well that i create main.c well if i ls c we can see that it’s going to find us
All of the files that have the c pop up prefix but what happens if we want just a single character dot c well we can use just the question mark character what happens if we just want two characters before the dot c so touch two two dot c
Well we can see two two dot c was not matched what will it match this yes it will but it will not match any of these as they do not have two unknown characters and so whenever you want to match any character or any explicitly sized group of characters before some known item
You can use a question mark so i think there are features in things like microsoft excel that have a similar notation to this uh and i think actually in um i think it’s sql when you have sql like commands where you do is uh select star where name is like uh
You know select star when name like then you could do like i don’t know ethern question mark marshall so this will match my name and it will match ethan k marshall it’ll march match ethan a martial but it will not match ethan atkinson marshall or something
That would uh that would be an invalid match it wouldn’t match that so the question mark simply matches any single character the star matches any string of characters and anything inside the brackets is a group of characters that can be matched and that’s the basics of shell globbing
The main thing to grasp is that they are not regular expressions this these are the only features that they have some say it’s limiting i have never met rhett any situation other than listing out dot files which would be useful to have a beginning anchor for but
Other than that i have never met a situation where shell globbing has let me down so i’m going to go back to the globbing directory and i’m going to remove everything in here using a glob again just to prove the point say yes and i’m going to show again uh the file that
I the command i used a minute ago to show this bracket command example as well and as i was doing that i also used uh command reverse searching which will be the next topic in fact so all you can see here is an example of a shorthand initializer this is in fact
Equivalent to this all that we’ve done is we’ve substituted the things between the commas and then placed it at the end and you can do this multiple times for instance i can do one two three four dot c and h and we can see that it’s going
To create one dot c one dot h dot and so each of these it will calculate every possible permutation between each of these so you can create marvelously complex things for instance i could do one two three four dot seed h dot test or if you have a comma and then
Nothing that means that there can be nothing there so i could do for instance maybe i could do hpp for creating c plus plus equivalence and we can now see that we have one dot c one dot cpp wonder h wonder hpp and so we can see that we’re starting to create
Enormously complex um patterns that can be created just by talking about all the different possibilities so you can create multiple possible examples of things by simply placing them in the brackets here and the shell will expand this into the argument list after you have typed it so let’s tackle a problem now uh
That problem is the fact that we often type the same commands over and over again and for me for instance this might be getting it it might be git status it might be get reset hard all of these commands are incredibly common and so as a result
They end up being stored in the history file many many times and my shell is actually configured to store every command once but have a separate file that stores references to commands that were done and then the history file actually sorts the commands by how recently they were typed and this allows the
Reverse search which on my shell is done by pressing ctrl r to search through the commands that i’ve recently typed and to substitute the ones that i’ve used the most often for instance make and sudo make install and so all that you’re doing when you’re typing typing a command like this is
You’re doing a reverse search through your entire shell’s history and it will substitute air to command for you now i think in bash you can do the exact same thing with control r and it will do similar to zsh which is my shell and uh
All that you do is you begin typing at the command for instance git reset hard i use quite often for uh distributed git management through emails for instance and so rather than typing out the same command over and over again instead you can just reverse search it by pressing
Ctrl r and you’re really getting the machines to do work for you a feature that people often underestimate is the feature of an alias because not only can you have aliases in your shell command files in your shell startup files you can create temporary aliases or change aliases temporarily
While you’re working on something so for instance if i am working on uh my program pubmed uh the go build command requires you to be at the root of the files infantry or sorry the make command which calls go build requires you to be at the root for environment variable reasons
I can’t just use go build unfortunately because it requires this for c go to work but i’ve have created an a shell function called cdr that will detect the root of the file system or the root of the git files tree and it will cd into it
So what i could do while i’m working on this is i could make an alias which is cdm an alias cdm to cdr make so if i go into ui components list dot go and i don’t know let’s fix this bug again and then cdm
We have seeded into the root and we have run make the only problem that we now have is that we are now at the root again so let’s change up our alias and let’s cd dash and hopefully this will work there might be some problems to do with the way that
The cd-r command works but you components and let’s revert this change that we just made because we obviously didn’t actually fix the bug and then cdm there we go so uh the use of aliases for temporarily and then you can on alias it once you’re done
The usage of aliases in order to make your life easier when doing specific tasks is an extremely powerful feature of the shell now in the beginning when i was talking about creating multiple directories at once or moving through multiple directories at once i showed the example of ce.semicoloncd.semicoloncd.dot
And i don’t properly explain this but this brings us onto the topic of shell error handling and command chains and this will also lead us onto the topic of pipelining but i’ll leave that for later on but every command that’s under unix and actually under windows although it’s disguised from you very cleverly
Has an exit code so for instance if i start up my uh podcast client and then attempt to run it we can see that we get an error saying that it’s already running so this command exits with a status which is not zero or a failed status this
Command here if i exit it will in fact exit with a zero status because the command exited successfully uh if i run pod bit again we can see that it exits successfully so we can actually detect this to do kind of pseudo error handling and to get
The shell to do things for us based on whether commands succeeded so if a command succeeded and you type at at the end of it this will tell the shell if this command succeeds then run this so what i’m going to do is echo and then some random text
In fact let’s make it success close it we can see that success has been echoed at the end here the inverse of this is the or or operator which says if this command failed then run this and we could say that it did not echo success at the end
If i open the client in the background again and attempt to run it we can see that it’s done success here because it failed this or here says run pod bit or if that failed run this then there’s also the neutral way of doing this which is
Regardless of the exit status just run this afterwards and that’s a semicolon so regardless of whether pod bit fails to run or whether it runs successfully success is going to be printed and so the semicolon is the kind of neutral equivalent of these so you can chain together long sequences of
Commands that all together uh decide whether uh you’re going to run another command afterwards and you can use this to do some online automation so for instance um ping so let’s say for instance you want to try and download a file but you’re not sure whether you have internet
Connection maybe your bar has disappeared mysteriously so what we could first do is we could do ping and then dash c tells us the count let’s take to do once and then let’s ping an ip that’s basically always up and we can see that ping transported 64 bytes
So because ping succeeded we can put an at at the end and we can say curl one http 1.101 and we can output that to 1.html and let’s run this command now and we can see that both commands have run and if we actually look at one.html we can see
That we get a cloudflare notice for some reason but the point is that the second command has run because the first one succeeded now this brings on to another interesting topic of sub shells as you might notice here that if we actually run this and we do an ore at the end the
Shell will interpret this as being if this curl fails so if the first command fails none of the stuff that comes after it will attempt to run and this could be a problem because what happens if we want to print an error message well
What we can do is we can tell the shell to spawn a subshell and this subshell groups together these commands in a shell instance and then does something else depending on whether that succeeds so we can then echo something else at the end and we can see it’s going to do
The cloud flow again if i disconnect from the internet device wi-fi and then disconnect will land zero we can see that will land zero will disconnect if i then run this command we can see that the first command will run in a sub shell and then the second command will
Execute because the first one failed and this is how you can have order of operations in the shell the penultimate thing i want to talk about is how you can get the shell to do maths for you so there are certain uh programs like calc
That you can get to do for you or bc-l which is just basically the same thing as calc but worse and both of these are perfectly good options i in fact have an alias with bc-lq where i just press mod f1 and it will open up a quick
Calculator in the corner for something that i need calculating for some reason when i did this i actually needed a calculator to do six times 90 but when you need to do get the shell to do this for you and assign it to a variable you can actually use this operation here
You can do double brackets i think actually it’s brackets like this and you can place the mathematics that you want it to do 10 x 20 and assign it to a variable and then echo doll sign a we can see that it’s done the calculation for us 10
Times 20 is 200. as you can see by the errors i was making i don’t use this very often mainly because of my calculator line that i have here but this is very useful if you’re writing shell scripts something that’s also very useful if you’re writing scripts are variables so
Variables in the shell are stored persistently for all of the time that your shell is running so for instance if i have a path like my go path this is actually stored by the go runtime and my startup so it’s in go root i think is it go
Path yep and we can see that it’s been set to my go folder where i install all the modules and put the source code for go project so this is a variable that’s been set and exported but not all variables have to be exported a minute ago i created a variable called
A which i can reference with dollar sign a so variables in the shell can be assigned by simply creating a name and then putting afterwards what you want to assign to them and this can be either arbitrary text i can do this or it can be the output of a command for
Instance and it’s important to note as well that if the output of the command could have spaces in it you should quote it uh double quotes interpret the dollar signs and also commands inside of them single quotes do not you can see the highlighting doesn’t show it
Interpreting it but for this purpose we don’t need to uh i’ll just put quotes here in case and set a equal to ls-la if we echo out a that should be dollar sign a we can see the output of ls-la is outputted so this is how you can assign to and use
Variables and these can be used anywhere in the parameter list for instance if i do unique and then i give to unique and this is the triple input operator we’ll talk about that next and i give it dollar sign a then unique is going to operate on this input data
So you can assign variables just setting them equal to they can be set to any data type including raw numbers and you can access them using dollar sign and then the variable in quotes and even you can create kind of pseudo aliases this way so for instance if i do
Be cat equals bat and then i do doll sign b cat and then we can see that it’s actually run the bat executable which is just a simple syntax highlighter you can also reassign variables to any value or to no value at all and it will simply be set to nil
I said a second ago that we’re going to talk about file redirection and while we’re talking about file redirection i think it would probably be very useful to talk about standard in and standard out and piping so uh pipes in unix take advantage of the fact that input and output few programs
Is done entirely using blocking file i o streams and i’m going to talk about that in the c tutorial series but for now all we need to know is that input is served into one file and output is put into another file one of those files i well two of those files output
And standard output and standard error are combined together and put into the terminal and standard input is simply a file that goes to the program and so by default standard input is just all of the lines that you type in your shell for so for instance if we type cat
Normally the cap program if we look at its posix manual page says that it will concatenate and print files uh and she’ll write them to standard outputs and that is what you can see on the screen so if you just run cat with no arguments you’ll be surprised to
Learn that it doesn’t actually error all it does is it connects the standard in to the standard out and so you can see that when i type a line it appears in our standard out here the cat program however also uses standard error so for instance if i tell
It to cat and then in unix when you type just a dash that means standard in and then i tell it to concatenate a file that obviously doesn’t exist then i type in standard io and then in unix as well when you press ctrl d on
Your keyboard that’s an end of file we can see that cat says asdf no such file or directory this is being printed in standard error and so we can actually get the output from a program and store it to a file using just this arrow here and so
We can type now into this program if we do test.out and we i’m now going to type this will appear in my file hello world type control d and we see cat asdf has still appeared here but the rest of our output as from before has not appeared why
Well because the output from this program has now appeared in test.out because the standard out of this process was set to test.out and this is a very very clever and very very elegant way of handling input and output in my opinion so you can get the output of any command and arbitrarily
Output it to a file but you’ll notice that our standard output here has still appeared on our terminal that’s because standard error was very specifically designed to separate itself from standard output because errors are not part of the main program’s output that need to be seen by
Other programs and we’ll see that in a minute when we do pipelining they need to be seen explicitly by the user so unless the user explicitly wants to put error messages in their output file as well the user is just going to see these if you want to output error messages for
Any reason what you do is you type 2 then an arrow and then test.error for instance this will now overwrite the file and it’s important to note that when you use this dash here it will overwrite and truncate any file that you put here any existing contents will be permanently lost
So you should be very careful we look at test dot error you can see that our error message has appeared it’s important to note as well that you’ll notice that no output from this command has appeared whatsoever this is what i typed in not even the error message has appeared
On our terminal because the error message went straight to this file and the output went straight to this file now what happens if we want to output both of these to the same location well in certain shells what you can actually do is put an at sign here and this is
Implicitly the same as outputting these to the same file but this is quite verbose to write uh because this is so far supposed to write i should say but unfortunately this is not posix compliant so if you use this in a shell script it will actually not work it will error
The posix way of doing this is to do at two and then this at one here says at the location that this goes to here it means send this to file descriptor one redirect to to file descriptor one and in fact we can do this with standard
In as well we can actually redirect standard out to standard error so if we do this same thing again we can actually these are both now appearing in our standard error so if we were to pipe this into another program uh xox arrow would echo would work better
We can see that actually this is appearing only from our cat here we’re not getting any output whatsoever from the echo program because our standard error is not going to this here so that is how file i o redirection works but i was going on about other programs
Seeing the outputs of files what was i talking about well what i was talking about was pipelining probably one of the most cool features of the unix operating system pipelining allows you to connect the input and output of multiple programs together in a line where the data flows
From left to right between each program as it sees the data this means that you can use multiple different programs to operate on the same piece of data each of which makes a little change about it and you can also take data from one source and you can
Pass it into another way of transforming it so let’s have an example here if i curl wttr.io and https i think this should be um oh no it’s it’s uh oh because i’ve got my internet off yes i’m such an idiot sometimes um if we cut out test.c
We can see the output of this program has appeared on the terminal and so this on its own seems pretty useless all we can do is look at the file until you realize actually that this can be shown as the input to another program and actually it’s often considered
Inefficient to use this as uh cutting a program and passing it to another program can easily be replaced with a shell input redirection or with um simply passing it as the file operand but what you can do is you can do this is just for an example mind don’t do this
In practice you can do cat test.c and then we can pipe it into the program bat which will syntax highlight it and then if we set the language to c we can see that bat is going to syntax highlight this correctly in practice we could just do bat dash lc test dot c
But this is much more efficient for longer processes for instance if we run ps-e we get every process on the entire system here and i’m not going to show it for long because it’s got information about the system in it but if we sort each of these by passing into
Another program called sort we can now sort these based on their process id so we can see the most recent process to be spawned was a k worker thread now what happens if we want to extract one of the fields out of these well what we
Can use is we can use awk and awk is another program which allows for the extraction of fields and what we can do is we can print dollar sign one and that will extract all of the process ids took a big uh quick break there to get a
Drink of water because uh talking for so long nearly half an hour has made my throat hurt but anyway we were getting the pits of all of the processes on our system and so this is an advantage as it allows us to pipe together a lot of programs
That can all do separate things with this and then finally at the end we can pipe into a program like less which will allow us to page through it or even vim which will accept it as input and allow us to edit at these pids so obviously it
Starts at pid one which will be in it and then we can start adding notes for instance and you can see how we can create enormously complex things that would otherwise be done with a program you know the windows way of doing this for instance would be to have a program
Called get pids maybe and then you’d have to have a program like the task manager that does this for you instead what i do is i just do this and then have these pids placed into a vm instance for me very simple and nice so before i move on
To the actual final thing i’m talking about because i’ve said i’m stopping at least twice now i want to cover some of the final little complexities about file redirection that i realized i forgot about so i talked about how to redirect standard out how to redirect standard error to standard out
Had to redirect both different files but i completely forgot about standard in so when we type in we get them sent to standard in but we can also send files to standard in so for instance that send let’s set the language to c and then send as the standard input file
Test dot c and we can see that it’s read from standard in and it’s got this file as its input and so this is a read-only copy it can’t actually change it if we do i don’t know like said and then just d delete everything uh said is going to delete every line
That gets passed to it if we pass in test.c it’s obviously going to print nothing but if we then bat out test.c again we can see that it’s all still there all that’s happening is each of the lines in this file will be read by the shell and will be sent to this
Process as it is asking for lines from this file another way that you can do this is often it’s very useful to send a command some arbitrary data so for instance there’s a program called tr that will allow you to translate between two character sets so a b uh fl i don’t know
Maybe it’ll send it to fl but often it’s very useful to send this arbitrary input for instance so maybe you want to disguise your email address like abab.com or something and it’s going to disguise it very cunningly like this what you can do is the triple redirect
Operator and so what will happen is the shell will create a temporary file in memory and then it will set it to the standard in of the process and so the standard in will read whatever is between these quotes hello world a b a b and the tr program
Is going to read it and it’s going to perform its operation on the input path between these quotes and because these are quotes you can do things like passing variables for instance uh let’s say i want to translate between go and translate it to i don’t know uppercase
And i’m going to pass it in dollar sign go path and we can see that it’s going to translate every instance of an o and then g into their upper case equivalence uh the actual program itself isn’t important what’s important is you’re sending it data using the triple redirect operator
So summarizing you can send data to a process using the input operator and you can send it an arbitrary string using the actual triple quote literal string input operator okay the final thing that i want to talk about is conditions and loops so there
Are a few loops that you can do in shell scripting and there are a few conditions that you can do and these mostly operate around the brackets binaries and these are actually executables by the way if we do rich dash a and then we’ll run this we can
Say it’s primarily a shell command but it’s also actually a program which i think is really cool and also there’s test which you know is uh also a binary and these programs are functionally equivalent and what they allow you to do is to do uh they allow you to do
Conditions in your shell script so when those initial conditions that we talked about using the or or or the and and are not good enough you can use the if operator so for instance if you want to check whether a string is 0 in length so for instance if dash if
Dash zed i think it is for some reason autocomplete isn’t working but if dash zed dull sign go path then go path equals dot fi and we can see i should just do something you can actually see on the screen but you can see this isn’t going
To work if i negate it using an exclamation point we can see that it’s going to work and this is saying if not if the value of gopath is not zero in length if in other words if it’s set to something then run this inside of here
And in reality obviously we could uh actually do this using a one line because it’s so small what you can do is you can do test. go path and then you can do and if test succeeds if the test will pass to it succeeds and go path is zeroing length then echo asdf
If it did not succeed replace these two with a pipe then we run asdf and this is much more clean and simple on a one line if you just want to do that you can also run a while loop which runs using the same kind of conditions as the
If does so for instance while um and there’s not really any good examples you can do this straight away but while and then some condition so for instance condition equals zero or zero um and then what you have to do is you have to do do and then echo sdf and then
At the end you put done and you can see it’s not going to run at all because the variable condition is not zero if we do condition equals zero and then run this while loop again it’s going to run infinitely and span out my terminal The final kind of loop is four which allows you to run through files and this is actually very useful if we go into our glob directory that we had from before if we run through and we want to for instance concatenate every single or let’s say we want to um
Change the name of every hpp file into just an h file so first i’m just going to remove all of the h files but now we have is a bunch of hpp files that we want to just be h files how can we do this
Well we could try and come up with some solution with globbing move star dot h pp to star.h but you’ll notice this isn’t going to work the auto expand isn’t working because this is not valid there are no h files so the glob is not going to match anything there are no hpp
Files that will match this either because this glob is here so there’s nothing we can do what we can instead do is we can do while uh we can do for f in and then what we can show afterwards is a list of files and so what we can do is
We do star dot hpp and then we’ve got a do here and anything inside of this loop here will now be run for every file name which matches this glob in f and so what will uh is after this in here is just a literal list of things
You can type these out literally in quotes it can be anything if you really want but it’s most useful for files and so what we can do is we can do move f and remember that this f will include the hthpp 2 and then what we’re going to do is move f to
Said we’re going to do said and we’re going to substitute dot hpp 4 dot h and then we’re going to pass it in dollar sign f done and that should read dollar sign f and we can now see that all of those hpp files have been moved to h files and
I’ve made a slight typo i didn’t include the dot h here in the uh set script but what we’ve done is we’ve cycled through a lot of files in a directory and we’ve operated on them uh you can also do for instance um uh in
Sequa and then 10 i think it is and then you can do printf d f and it should just be a string here and i don’t really know what i’ve done there that should be a dollar sign there we go so sequence is going to generate the sequence for us and i think
This actually can be a d but we need a n here and uh what we’ve done is we’ve done a sequence from 0 to 10 or from 1 to 10 in this case and so this is the bash equivalent or the shell equivalent of a
Very simple for loop and so you can have any sequence of things here you can do f in a b c and d i’m going to change this to a string and it’s just going to cycle through a b c and d and so a for loop will split
This along white space and it will go through each one of the things past after the in here placing its value into f for each iteration now this is the third time i’ve said final thing but i just thought of one last thing when we talked about the at
Operator this is very simple to another operator which is the asynchronous start operator and what this does is it tells a process to start in the background and to stay there for instance we run vim at we can see that vim has started in the background and it got suspended when it
Attempted to write to the tty because obviously we’re not seeing the results of it being to the tty because it’s in the background but you’ll notice that when we run vim we now have a prompt that we can actually do all the work with we can run other commands we can
Look at other things in the directory and if we run ffg we can get this process back into the foreground and we can quit it normally and you can also run bg which tells it to continue in the background in this case it wouldn’t actually work because
It would just get suspended again when it tries to write to the terminal because progresses that try to write the terminal get suspended until you give them their attention but this is the whole topic of job control and you can look at what jobs you have running in the background with just jobs
If i run while true do and i just do done and then put an at the end we can see that this is going to run in the background it’s going to take up all my cpu taking a bunch of cpu cycles but if we run jobs we can see that while
True do nothing done is running still and what we can do is we can do background percent one and it’s going to tell us it’s already in the background but say that this wasn’t and we just suspended it we can tell it to continue and if you have a process that’s running
For instance vim and you press ctrl z what it’s going to do is you’re going to get suspended and what you can then do is you can tell it’s going to the background and it’s going to attempt to continue in the background and then it’s going to get suspended when it tries to
Write to the tty and so if we look at our jobs again we can see that now we have a running process which is infinitely doing nothing and we have a suspended neoven process so to get rid of these what we can do is we can kill and then the process ids of
Each of your running jobs are stored in variables that start with a percent sign so if we kill the process that’s running at job one that’s in brackets here we run percent one and we have now had this job terminated and it’s going to tell us that job one
Has terminated if we run jobs we can see there’s no longer anything running um if we do this again and instead of doing this asynchronously using the at operator we instead run it in the foreground if we press ctrl z for some reason that didn’t work
But if this was a real process we could run ctrl z and it would interrupt the process and it would place it in the background to continue its work and so i often do this with for instance ffmpeg scripts uh that i have piped out to devnull uh
So that i don’t get output spamming my terminal when you pipe out two slash dev slash null it doesn’t get suspended for tty output and that’s a very important distinction to make but the at sign is very important for when you want to do asynchronous programming
And so that’s the shell and that’s all of the features that i use on a day-to-day basis that i find incredibly useful and they really make you more efficient when you know how to use them so you know i can run multiple stupid in a row
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
my cdr:
“`
alias cdr='cd "$( git rev-parse –show-toplevel )"'
“`
I also find these useful:
“`
alias cd1='cd ..'
alias cd2='cd ../..'
alias cd3='cd ../../..'
alias cd4='cd ../../../..'
alias cd5='cd ../../../../..'
alias cd6='cd ../../../../../..'
“`
You should take a look at ripgrep, its much faster than ack.
Clear and efficient, thanks!
good content
Love your videos. Hope the YT gods push your content to more people.
Nice tutorial! Keep it up. ❤️
How did you get your new terminal open in the same dir as existing terminal?
Thanks man!
great video man, hope you’re doing well
What A level subjects did you do?
Seems like combining GUI & CLI is gonna give you the worst of both worlds unless you do some text-user-interface magic and are willing to live with the lack of "pretty" styling and productivity when "immersed" inside the GUI without custom keyboard shortcuts or mouse gestures with the benefit of being productive when using pipelineable commands.
I love your videos, thanks for posting. And can you upload your videos to Odysee?
nice tips. thank you very much