{"id":46,"date":"2018-10-13T20:00:00","date_gmt":"2018-10-13T12:00:00","guid":{"rendered":"https:\/\/yeslq.com\/20181046.html"},"modified":"2018-11-11T16:11:00","modified_gmt":"2018-11-11T08:11:00","slug":"the-linux-command-line-editing-the-scripts-you-already-have","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=46","title":{"rendered":"The Linux Command Line&#8212;Editing The Scripts You Already Have"},"content":{"rendered":"<h1 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\"><i style=\"font-size: x-small;\"><a href=\"http:\/\/linuxcommand.org\/tlcl.php\" style=\"color: #002740;\">The Linux Command Line&nbsp;<\/a><\/i><span style=\"font-size: xx-small;\">by William Shotts<\/span><\/h1>\n<div><span style=\"font-size: xx-small;\"><br \/><\/span><\/div>\n<div><span style=\"font-size: xx-small;\"><\/span><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\"><span style=\"font-size: xx-small;\">Before we get to writing new scripts, I want to point out that you have some scripts of your own already. These scripts were put into your home directory when your account was created, and are used to configure the behavior of your sessions on the computer. You can edit these scripts to change things.<\/span><\/div>\n<p><span style=\"font-size: xx-small;\"><\/span><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\"><span style=\"font-size: xx-small;\">In this lesson, we will look at a couple of these scripts and learn a few important new concepts about the shell.<\/span><\/div>\n<p><span style=\"font-size: xx-small;\"><\/span><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\"><span style=\"font-size: xx-small;\">During your session, the system is holding a number of facts about the world in its memory. This information is called the&nbsp;<i>environment<\/i>. The environment contains such things as your path, your user name, the name of the file where your mail is delivered, and much more. You can see a complete list of what is in your environment with the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/seth.html\" style=\"color: #002740;\">set<\/a><\/tt>&nbsp;command.<\/span><\/div>\n<p><span style=\"font-size: xx-small;\"><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Two types of commands are often contained in the environment. They are&nbsp;<i>aliases<\/i>&nbsp;and&nbsp;<i>shell functions<\/i>.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">How Is The Environment Established?<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">When you log on to the system, the bash program starts, and reads a series of configuration scripts called&nbsp;<i>startup files<\/i>. These define the default environment shared by all users. This is followed by more startup files in your home directory that define your personal environment. The exact sequence depends on the type of shell session being started. There are two kinds: a&nbsp;<i>login shell session<\/i>&nbsp;and a&nbsp;<i>non-login shell session<\/i>. A login shell session is one in which we are prompted for our user name and password; when we start a virtual console session, for example. A non-login shell session typically occurs when we launch a terminal session in the GUI.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Login shells read one or more startup files as shown below:<\/div>\n<table border=\"\" cellpadding=\"8\" style=\"background-color: white; color: black; font-family: verdana, arial, helvetica, sans-serif;\" summary=\"Startup Files For Login Shell Sessions\">\n<tbody>\n<tr>\n<td valign=\"top\"><strong>File<\/strong><\/td>\n<td valign=\"top\"><strong>Contents<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">\/etc\/profile<\/tt><\/td>\n<td valign=\"top\">A global configuration script that applies to all users.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">~\/.bash_profile<\/tt><\/td>\n<td valign=\"top\">A user&#8217;s personal startup file. Can be used to extend or override settings in the global configuration script.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">~\/.bash_login<\/tt><\/td>\n<td valign=\"top\">If&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">~\/.bash_profile<\/tt>&nbsp;is not found, bash attempts to read this script.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">~\/.profile<\/tt><\/td>\n<td valign=\"top\">If neither&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">~\/.bash_profile<\/tt>&nbsp;nor&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">~\/.bash_login<\/tt>&nbsp;is found, bash attempts to read this file. This is the default in Debian-based distributions, such as Ubuntu.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Non-login shell sessions read the following startup files:<\/div>\n<table border=\"\" cellpadding=\"8\" style=\"background-color: white; color: black; font-family: verdana, arial, helvetica, sans-serif;\" summary=\"Startup Files For Non-Login Shell Sessions\">\n<tbody>\n<tr>\n<td valign=\"top\"><strong>File<\/strong><\/td>\n<td valign=\"top\"><strong>Contents<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">\/etc\/bash.bashrc<\/tt><\/td>\n<td valign=\"top\">A global configuration script that applies to all users.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">~\/.bashrc<\/tt><\/td>\n<td valign=\"top\">A user&#8217;s personal startup file. Can be used to extend or override settings in the global configuration script.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">In addition to reading the startup files above, non-login shells also inherit the environment from their parent process, usually a login shell.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Take a look at your system and see which of these startup files you have. Remember\u2014 since most of the file names listed above start with a period (meaning that they are hidden), you will need to use the \u201c-a\u201d option when using ls.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">~\/.bashrc<\/tt>&nbsp;file is probably the most important startup file from the ordinary user\u2019s point of view, since it is almost always read. Non-login shells read it by default and most startup files for login shells are written in such a way as to read the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">~\/.bashrc<\/tt>&nbsp;file as well.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">If we take a look inside a typical&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">.bash_profile<\/tt>&nbsp;(this one taken from a CentOS 4 system), it looks something like this:<\/div>\n<p><tt style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\"><\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"># .bash_profile<br \/># Get the aliases and functions<br \/>if [ -f ~\/.bashrc ]; then<br \/> . ~\/.bashrc<br \/>fi<br \/><br \/># User specific environment and startup programs<br \/>PATH=$PATH:$HOME\/bin<br \/>export PATH<br \/><\/pre>\n<p><\/tt><span style=\"background-color: white; font-family: &quot;verdana&quot; , &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;\"><\/span><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Lines that begin with a \u201c#\u201d are comments and are not read by the shell. These are there for human readability. The first interesting thing occurs on the fourth line, with the following code:<\/div>\n<p><tt style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\"><\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">if [ -f ~\/.bashrc ]; then<br \/> . ~\/.bashrc<br \/>fi<br \/><\/pre>\n<p><\/tt><span style=\"background-color: white; font-family: &quot;verdana&quot; , &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;\"><\/span><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">This is called an&nbsp;<i>if compound command<\/i>, which we will cover fully in a later lesson, but for now I will translate:<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">If the file &#8220;~\/.bashrc&#8221; exists, then read the &#8220;~\/.bashrc&#8221; file.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">We can see that this bit of code is how a login shell gets the contents of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">.bashrc<\/tt>. The next thing in our startup file does is set set PATH variable to add the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">~\/bin<\/tt>&nbsp;directory to the path.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Lastly, we have:<\/div>\n<p><tt style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\">export PATH<\/tt><span style=\"background-color: white; font-family: &quot;verdana&quot; , &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;\"><\/span><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/exporth.html\" style=\"color: #002740;\">export<\/a><\/tt>&nbsp;command tells the shell to make the contents of PATH available to child processes of this shell.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">Aliases<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">An alias is an easy way to create a new command which acts as an abbreviation for a longer one. It has the following syntax:<\/div>\n<p><tt style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\">alias&nbsp;<i>name<\/i>=<i>value<\/i><\/tt><span style=\"background-color: white; font-family: &quot;verdana&quot; , &quot;arial&quot; , &quot;helvetica&quot; , sans-serif;\"><\/span><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">where&nbsp;<i>name<\/i>&nbsp;is the name of the new command and&nbsp;<i>value<\/i>&nbsp;is the text to be executed whenever&nbsp;<i>name<\/i>is entered on the command line.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Let&#8217;s create an alias called &#8220;l&#8221; and make it an abbreviation for the command &#8220;ls -l&#8221;. Make sure you are in your home directory. Using your favorite text editor, open the file&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">.bashrc<\/tt>&nbsp;and add this line to the end of the file:<\/div>\n<pre style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\"><br \/>alias l='ls -l'<br \/><\/tt><br \/><\/pre>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">By adding the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/aliash.html\" style=\"color: #002740;\">alias<\/a><\/tt>&nbsp;command to the file, we have created a new command called &#8220;l&#8221; which will perform &#8220;ls -l&#8221;. To try out your new command, close your terminal session and start a new one. This will reload the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">.bashrc<\/tt>&nbsp;file. Using this technique, you can create any number of custom commands for yourself. Here is another one for you to try:<\/div>\n<pre style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\"><br \/>alias today='date +\"%A, %B %-d, %Y\"'<br \/><\/tt><br \/><\/pre>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">This alias creates a new command called &#8220;today&#8221; that will display today&#8217;s date with nice formatting.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">By the way, the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">alias<\/tt>&nbsp;command is just another shell builtin. You can create your aliases directly at the command prompt; however they will only remain in effect during your current shell session. For example:<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">alias l='ls -l'<\/tt><\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">Shell Functions<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Aliases are good for very simple commands, but if you want to create something more complex, you should try&nbsp;<i>shell functions<\/i>&nbsp;. Shell functions can be thought of as &#8220;scripts within scripts&#8221; or little sub-scripts. Let&#8217;s try one. Open&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">.bashrc<\/tt>&nbsp;with your text editor again and replace the alias for &#8220;today&#8221; with the following:<\/div>\n<pre style=\"background-color: white; font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\"><br \/>today() {<br \/>    echo -n \"Today's date is: \"<br \/>    date +\"%A, %B %-d, %Y\"<br \/>}<br \/><\/tt><br \/><\/pre>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Believe it or not,&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/functionh.html\" style=\"color: #002740;\">()<\/a><\/tt>&nbsp;is a shell builtin too, and as with&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">alias<\/tt>, you can enter shell functions directly at the command prompt.<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt> <tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">today() {<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace;\">&gt;<\/tt> <tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo -n \"Today's date is: \"<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace;\">&gt;<\/tt> <tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">date +\"%A, %B %-d, %Y\"<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace;\">&gt;<\/tt> <tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">}<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">However, again like&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">alias<\/tt>, shell functions defined directly on the command line only last as long as the current shell session.<\/div>\n<p><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts Before we &#8230; <a title=\"The Linux Command Line&#8212;Editing The Scripts You Already Have\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=46\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Editing The Scripts You Already Have\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,6],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-linux","category-shell","category-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=46"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":107,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/46\/revisions\/107"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}