What is C shell (csh)?

Learn via video courses
Topics Covered

Overview

The C shell in Linux, generally known as csh, is a Unix-like command-line interpreter and scripting language. It was created in the late 1970s by Bill Joy at the University of California, Berkeley, and is regarded as one of the first Unix shells. In a Linux system, the C shell provides a robust and flexible environment for executing commands, scripting, and automating processes.

In this article, we will learn everything about C Shell in Linux from its usage to its syntax, flags and files.

What is C Shell?

C Shell in Linux, abbreviated csh, is a command-line shell and scripting language for Unix-like operating systems. As a shell, csh serves as an interface between the user and the operating system, allowing users to interact with the system by typing commands. It supports command execution, file and directory management, process control, and script execution.

The C shell in Linux is a scripting language as well as an interactive environment. It allows users to create unique instructions, automate activities, and conduct complex procedures by writing scripts. Variables, control structures such as loops and conditionals, file manipulation, input/output redirection, and command replacement are among the scripting features of csh.

Although the C shell is not as commonly used today as some other shells, it still has a dedicated user base and continues to be available in Unix-like systems. Additionally, an enhanced version called tcsh (Tenex C shell) was developed to address some limitations of the original C shell and provides improved interactive features and scripting capabilities.

How to Use C Shell?

To use the C shell in Linux (csh), follow these steps:

  • Launch a terminal emulator on your Linux system. This can typically be found in the applications menu or accessed through a keyboard shortcut like Ctrl+Alt+T.
  • If csh is not installed in your system, installed it by following the command.

C-Shell

  • Once it is installed, type csh or tcsh (the enhanced version of the C shell) and press Enter. This command starts a new C shell session.
  • When you run the csh command, it launches the C Shell (csh) interactive shell. In the example above, the % symbol represents the shell prompt, indicating that the csh shell is ready to accept commands.

C-Shell-1

  • Use the C Shell according to your requirements.

Syntax

The basic syntax of Invoking the C Shell in Linux is as follows:

Let's Understand the syntax of C Shell below:

  • The shell executes commands from a file or interactively from a terminal keyboard. The command csh launches the C shell.
  • When you run the csh command, it searches your home directory for the .cshrc file (which stores customised user information) and executes commands from it if it exists in that file. If you run the csh command as a login shell, it will execute commands from your .cshrc and .login files.
  • If neither the -i, -c, -s, or -t flags are provided and the File [Parameter] is specified, the shell runs the script file designated by the File [Parameter], including any parameters specified. The given script file must have read permissions for the logged-in user. The shell disregards any setuid and setgid settings.

Important Notes:

  1. If you use the csh command with the -c or -s flags, you should not specify a script file. If you supply a script file, the programme opens it and stores the script file name for future use by $0 (dollar sign, zero). The script will subsequently be executed by csh. The remaining arguments set up the argv variable.
  2. If the C shell is already running, you can read the .cshrc file again by typing source Pathname, where Pathname is the path to the .cshrc file.
  3. To prevent issues with remote operations, the .cshrc file should not include any functions that echo output unless they check for the promptvariable,whichindicatesthattheshellisinteractive.Otherwise,ifadistantsystemexecutesacommandsuppliedbythelocalsystem,boththecommandandtheshellareexecuted.ExeccshrcptFilename,forexample,runsthe.cshrcfileandconsiderstheechoedoutputtobetheintendedresponse.Tocheckfortheprompt variable, which indicates that the shell is interactive. Otherwise, if a distant system executes a command supplied by the local system, both the command and the shell are executed. Exec csh rcp -t Filename, for example, runs the .cshrc file and considers the echoed output to be the intended response. To check for the prompt variable, use an if clause.

Features of C Shell

The C shell in Linux offers several features that make it a powerful tool for working in a Linux environment. Let's explore some notable features:

  • Interactive Shell: The C shell provides a real-time interactive shell environment in which users may enter commands and interact with the system. It can execute commands, perform input/output activities, and manage jobs.
  • Command History: The C shell maintains a history of previously executed commands. This feature allows users to easily recall and re-execute commands without having to type them again. The command history can be accessed using keyboard shortcuts or specific commands.
  • Job Control : C shell supports job control, which enables users to manage multiple running processes. With job control, you can start processes in the background, bring them to the foreground, suspend or resume their execution, and terminate them.
  • Command Aliasing : The C shell allows you to create command aliases, which are custom names or shortcuts for longer or frequently used commands. Aliases can be defined using the alias command, making it easier to invoke complex or lengthy commands by using a simple alias.
  • Scripting Language : The C shell is not just an interactive shell but also a scripting language. It supports programming constructs such as variables, control structures (loops and conditionals), and functions. This enables users to write scripts to automate tasks, create custom commands, and perform complex operations.
  • File Globbing : File globbing, also known as file expansion or wildcard matching, is a feature that allows you to specify file patterns using wildcards. The C shell supports wildcards like * (matches any sequence of characters) and ? (matches any single character). This feature is particularly useful when working with multiple files or performing batch operations.
  • Input/Output Redirection : The C shell provides input/output redirection capabilities, allowing you to redirect the standard input, output, and error streams of commands. You can redirect input from a file, redirect output to a file, or even pipe the output of one command as input to another using the | symbol.
  • Command Substitution : Command substitution is a feature that allows you to use the output of a command as part of another command. The C shell provides two syntaxes for command substitution: using backticks or the $() syntax. Command substitution is useful when you need to dynamically generate arguments or provide command output as input to another command.
  • Shell Variables : The C shell allows you to define and use shell variables to store and manipulate data. Variables can be set using the set command and accessed using the $ prefix. They can hold strings, numbers, or other data types, providing a convenient way to store and reuse values within scripts or interactive sessions.
  • Interactive Editing : The C shell provides interactive editing capabilities for command-line editing and modification. You can use keyboard shortcuts and commands to move the cursor, edit commands, and recall previous commands from the history. This feature makes command entry and editing more efficient.

C Shell Flags

Here are all the flags or options available in the C shell in Linux (csh):

FlagDescription
[-v]The verbose shell variable is set, which causes command input to be echoed following history replacement.
[-V]Sets the verbose shell variable before running the.cshrc file.
[-x]Sets the echo shell variable to ensure that commands are echoed after all replacements and before they are executed.
[-X]Sets the echo shell variable before the.cshrc file is loaded.
[-e]Exits if any called command fails or returns a nonzero exit status.
[-f]Starts the C shell without looking for or running commands from your home directory's .cshrc file.
[-i]Even though input does not appear to be coming from a workstation, it prompts for its top-level input (an interactive shell).
[-n]Commands are parsed but not executed. This flag assists you in examining the syntax of shell procedures.
[-c]Reads commands from the only argument that must be provided. The argv variable is used to store any remaining arguments.
[-s]Commands are read from standard input.
[-t]This function reads and processes a single line of input. To continue into another line, use a (backslash) to escape the new-line character at the end of the current line.
[-b]Forces a break in option processing, resulting in any additional shell arguments being regarded as non-option arguments. This flag can be used to pass options to a shell script without causing any misunderstanding or potential for deception.

Files

In the C shell (csh), several files can be used to configure and customize the shell's behaviour.

FileDescription
~/.cshrcEach shell reads this at the start of execution. The.cshrc file is completely customizable.
~/.loginAt login, the login shell reads this file after the .cshrc file.
~/.logoutThis file is read by the login shell at the logout part
/usr/bin/shThis file contains the path of the default shell.
/tmp/sh*This file contains the temporary file for <<.
/etc/passwdThis file contains the source of home directories for the ~File parameter.

Conclusion

  • csh or C Shell in Linux acts as an interface between the user and the operating system, allowing users to interact with the system by entering commands.
  • It provides features such as command execution, file and directory management, process control, and script execution.
  • We can use C Shell by using csh in the terminal.
  • We learned the syntax of using the csh command for invoking the C Shell.
  • Then we looked at some important features of C Shell.
  • After that, we understood C Shell flags.
  • Then we looked at the files available in C Shell