GRAPES

"We strive to provide Good, Reliable, Advanced, Professional, Enriching & Safe (GRAPES) experience by our products & services"

The “.cshrc” file in a tcsh shell

The “.cshrc” file in a tcsh shell

The “tcsh” shell is a unix shell based on and backward compatible with the C shell.  The terminal which’s an application for running unix shells like tcsh, bash, zsh..etc is one of the key features of a Mac system.  The shell lets you run unix command using the terminal and lets you automate things. It’s rightly said that engineers would love the terminal and the shell’s capabilities as it lets you create a script to run repetitive tasks.  There may types of shells and we are not going to dwell into that topic in this post.  But, I want to start with tcsh as it’s the first shell I learnt, specifically we’ll discuss about the “.cshrc” file which can used to source environment variables, define aliases, add few more info to your shell prompt..etc.  This file gets sourced when you open a tcsh shell/terminal.

Location of the “.cshrc” file

  • We can use one of the text editors like “Vi” or “Vim” to create this file.  This file needs to be located in the user’s home directory on the unix file system.  It’s important to note that this is a hidden file and the file name starts with a dot (“.”) indicating that it’s hidden.
  • In other-words, if you go and run a “ls -ltr” command in your home directory then you won’t see this file

    Output of ls -ltr run inside a home directory

  • And you need to run “ls -ltra” (‘a’ is for all files both hidden and normal) to see the .cshrc file

    Output of ‘ls -ltra’ showing the .cshrc file

     

Contents of the “.cshrc” file

Aliases:

  • You could use the alias command to alias a text or a string to a specific set of characters.  As an example, I have put the below 3 aliases in my .cshrc file which will let the shell execute the full command when I type in just the alias.

    Alias examples

     

    Shell executing “ls -ltr” using the alias “l”

Adding more info to the command prompt

  • You can add more information like username, date or time to the command prompt as well.  This makes the command prompt look cool with added information.  For example the below 2 commands sets the left prompt and right side prompt information

    Commands for the left & right prompts in the .cshrc file

     

  • The first command causes the full path of the directory we are in to be displayed in the blue colour on the left and the second command causes the date in the format DD-Month-YYYY and time in the format “HH:MM:SS” to be displayed in white colour on the right.

    Left & Right prompts in a terminal

     

Local & Environment variables

  • You can also set local and environmental variables using the “.cshrc” file.  For example, the below two commands sets the variable “rmstar” and the environmental variable “PATH”

    Examples for manipulating variables using “.cshrc” file

  • The rmstar variable causes a warning message to be displayed when the user executes “rm *” command (which means remove every file) asking whether do you really want to delete all the files.  It’s very useful to avoid inadvertent running of “rm *” command which could wipe off the entire suite of files.

    Warning message of “rm *” command

  • The PATH environment variable is very useful feature of the shell.  Whenever the user executes a command from the command prompt, the shell looks into the “PATH” environment variable to find out the whereabouts of the command executable.  If the user enters a command (say ‘junk’) whose executable isn’t present in any of the paths defined in the “PATH” environment variable then it issues an error saying “Command not found.”  This feature is very useful to add your own suite of scripts you have developed and just point the PATH variable to include the directory path of your scripts (in my case, it’s in the path “/Users/amjad/scripts”).  Now for executing any command or script located in that folder, I need not type the entire path of the script on the prompt.  Rather, I just need to type the name of the command and shell would use the PATH variable to locate the directory.  Yes, it’s very useful 😀

    Example of a command whose path is not known

     

Yes, you could do lot more using the “.cshrc” file and the above are some examples of its features.  Feel free to give 👍 to the post!

3
Avatar photo

Amjad

Website: https://blog.al-zakirah202.familyds.com

I'm Amjad from Bangalore in India. I got a Bachelor's degree in Electronics & Communications from National Institute of Engineering in 2004. I currently work as Product & Test Engineer at a Semiconductor Company. I am currently married with 3 kids - fortunate to be blessed with a good wife as well! I enjoy programming and automating things wherever possible. I have presented many papers and delivered talks in technical conferences. Among my hobbies are setting up home servers for media, photo, books and others, language learning, electronic circuit analysis, software development and many more..

Leave a Reply

Your email address will not be published. Required fields are marked *