What are interactive and non-interactive shells in Linux

Linux School Tech
5 min readJan 20, 2024

--

Interactive shell in linux and Unix like OS

An interactive shell in Linux is a shell that allows you to type commands and receive output from those commands. This is essentially a shell with which you interact. Examples include your login shell and any other shells you start manually. When you run a shell script, a non-interactive shell is started that runs the commands in the script, and then exits when the script finishes.

An interactive shell is defined as the shell that simply takes commands as input on tty from the user and acknowledges the output to the user. This shell also reads startup files that occurred during activation and displays a prompt. It also enables job control by default. An interactive script is a script that requires input from the user. Interactive scripts couldn’t run in the background as they required input from the user.

You can start an interactive shell by giving the name of the shell after you logged into the system. For example, you can start an interactive shell by typing `bash` in the terminal.

It’s worth noting that interactive non-login shells (those you start manually from another shell or by opening a terminal window) don’t read your .login or .profile files. These are only read and executed by login shells (shells started by the login system process, or by your X display manager), so the commands and settings they contain are only applied once, at the beginning of your login session.

Non-interactive shell in linux and Unix like OS

A non-interactive shell in Linux is a shell that executes commands without requiring any user interaction. This type of shell is typically used for running scripts or automated processes. Unlike an interactive shell, a non-interactive shell doesn’t prompt the user for input or display output to the user.

In a non-interactive shell, `.bashrc` and `.profile` files do not get executed. This is because these files usually contain setup commands that are meant to be run when a user logs in, such as setting environment variables or defining aliases. Since a non-interactive shell doesn’t have a user to log in, these files aren’t needed.

The non-interactive shell also influences the `PATH` variable, which determines where the shell looks for executables. It’s often recommended to use the full path for a command in non-interactive shells to avoid any potential confusion about where the command is located.

Non-interactive scripts can run smoothly in the background, making them ideal for long-running processes or tasks that need to complete without user interaction. Scripts like init and startup are considered non-interactive because they must run without human intervention.

Here’s an example of a script that would be suitable for a non-interactive shell. This script backs up a directory to a tarball:

#!/bin/bash

# Directory to backup
dir="/path/to/directory"

# Tarball name
tarball="backup.tar.gz"

# Backup directory to tarball
tar -czf $tarball $dir

# Print message indicating success
echo "Backup completed successfully."

This script doesn’t require any user interaction, so it’s suitable for a non-interactive shell. It automatically backs up a specified directory to a tarball and prints a success message.

Here’s how to use it:

  1. Save the script to a file, for example, backup.sh.
  2. Make the script executable with the command chmod +x backup.sh.
  3. Run the script with the command ./backup.sh.

This script is a good example of a non-interactive script because it performs a task automatically without waiting for user input.

What tasks can be performed using a non-interactive shell script?

Non-interactive shell scripts are particularly useful for performing tasks that need to be done regularly, such as:

1. Automated Backups: As shown in the previous example, non-interactive shell scripts can be used to automate backups of important directories or databases. This ensures that data is regularly saved and can be recovered if necessary.

2. Log Analysis: Shell scripts can be used to analyze server logs, looking for specific patterns or errors. This can help with identifying issues and understanding the behavior of the system over time.

3. File Operations: Non-interactive shell scripts can perform various file operations like moving, renaming, or deleting files based on certain conditions. For example, a script could delete old log files that are no longer needed.

4. System Monitoring: Shell scripts can be used to monitor system resources like CPU usage, memory usage, disk space, etc. This information can be sent to a monitoring tool for analysis and alerting purposes.

5. Automated Testing: Non-interactive shell scripts can be used to automate testing of software applications. This can include unit tests, integration tests, performance tests, etc.

6. Task Scheduling: Shell scripts can be scheduled to run at specific times using tools like cron. This can be used to perform regular maintenance tasks, data updates, etc.

7. Network Operations: Shell scripts can be used to automate network operations like checking connectivity, scanning ports, managing firewall rules, etc.

These are just a few examples. The possibilities are virtually limitless and depend largely on the specific needs of the system or application.

Difference between intractive and non-intractive shells

The main difference between interactive and non-interactive shells lies in how they handle user input and output.

Interactive Shell:

An interactive shell allows users to interact with the system by entering commands manually. The shell waits for user input, executes the command, and displays the output to the user. This is typically used when you’re working directly with a terminal or command-line interface. Interactive shells read startup files like `.bashrc` and `.profile` during initialization. They enable job control by default and display a prompt to indicate that they’re ready for input.

Non-Interactive Shell:

A non-interactive shell executes commands without user interaction. It’s often used for running scripts or automated processes. Non-interactive shells don’t read `.bashrc` and `.profile` files, and they don’t display a prompt. Instead, they often write output to a log file. They can also run in the background without requiring user input. Scripts like `init` and `startup` are examples of non-interactive shells.

In summary, the key difference between interactive and non-interactive shells is that interactive shells allow user interaction, while non-interactive shells do not.

My YouTube Channel

More shell script videos and linux tutorials on my YouTube Channel.

--

--