- How to Run a Program from the Command Line on Linux
- Running a Program From the Terminal
- Running a Program Outside the $PATH Variable
- Adding a Directory to your $PATH Variable
- Installing and Uninstalling a Program in the Terminal
- Expert Q&A
- Tips
- You Might Also Like
- How To Execute a Command with a Shell Script in Linux
- Prerequisites
- Getting Started
- Creating and Running a Basic Shell Script
- Using Variables in Shell Scripts
- Reading Input from the Command Line
- Defining Functions
- Conclusion
How to Run a Program from the Command Line on Linux
This article was co-authored by wikiHow staff writer, Travis Boylls. Travis Boylls is a Technology Writer and Editor for wikiHow. Travis has experience writing technology-related articles, providing software customer service, and in graphic design. He specializes in Windows, macOS, Android, iOS, and Linux platforms. He studied graphic design at Pikes Peak Community College.
The wikiHow Tech Team also followed the article’s instructions and verified that they work.
This article has been viewed 278,602 times.
Most Linux distributions have a graphical user interface that allows you to open programs by just clicking on the program’s icon in the Apps menu. However, there are situations where you may want to run a program from the Terminal. The Terminal is a powerful tool that allows you to run programs and manage your Linux system using keyboard commands. This wikiHow teaches you how to run a program from the Terminal in Linux.
Running a Program From the Terminal
Press Ctrl + Alt + T to open the Terminal. You can open the Terminal using the keyboard shortcut on most Linux distributions. The keyboard shortcut is Ctrl + Alt + T. You can also click the Terminal icon in your Apps menu. It generally has an icon that resembles a black screen with a white text cursor.
- For example, if you want to run Firefox from the Terminal, you would simply type firefox and press Enter.
- Type -h or —help after the program name to display the help menu for that program. Many programs have additional command modifiers you can use to launch the program in a specific way. For example, you can launch a website in a web browser by typing the web browser name followed by the web address and press Enter to launch that website in the web browser (i.e. firefox www.wikihow.com .
- If you receive a message that says you don’t have permission to run a program or access is denied, type sudo before the program name and press Enter. The «sudo» command allows regular users to run Terminal commands with administrative privileges or root access.
- If you want to run a C or C++ program from the Terminal, you will first need to complie the program before you can launch it from the Terminal.
Running a Program Outside the $PATH Variable
Press Ctrl + Alt + T to open the Terminal. You can open the Terminal using the keyboard shortcut on most Linux distributions. The keyboard shortcut is Ctrl + Alt + T. You can also click the Terminal icon in your Apps menu. It generally has an icon that resembles a black screen with a white text cursor.
Type cd followed by a space and the location the program file is saved to. If the program launch file is saved to a location that is not in your $PATH variable, then you will need to navigate to that location inside the Terminal. You can do so using the «cd» command. For example, if you have folder for Python programs saved in your «Documents» folder, you can navigate to it in the Terminal by typing cd ~/Documents/Python or something similar, and then press Enter.
Type chmod a+x [filename] and press ↵ Enter . Replace «filename» with the actual launch file of the program. The «chmod a+x» command tells Linux the file is an executable file. [1] X Research source
Type «./» followed by the launch filename and press ↵ Enter . This launches the program. For example, if you have a Python file called «Helloworld.py», you would type ./helloworld.py to launch the file. [2] X Research source
Adding a Directory to your $PATH Variable
Press Ctrl + Alt + T to open the Terminal. If you cannot run a program by simply typing the program’s name, you may need to add the directory that the program is installed in to your $PATH variable. You can do this from the Terminal as well. Use the keyboard shortcut to open the Terminal if you haven’t already done so.
- For example, if you have a program installed in the «bin» directory of your «Home» folder, you would type export PATH=$PATH:$Home/bin and press Enter. This will temporarily add the «$Home/bin» directory to your $PATH variable.
- You can see which directories are currently added to your $PATH variable by typing the command echo $PATH and pressing Enter.
Type nano ~/.bashrc and press ↵ Enter . This opens the «.bashrc» file in a text editor that is based in the Terminal. You can use this file to permanently add the directory to your $PATH variable.
Add the «export PATH» command to the file. To do so, scroll down to the bottom of the file using the mouse wheel and type export PATH=$PATH:[path/to/program] at the bottom. Replace «[path/to/program]» with the actual directory tree the program is installed in. [3] X Research source
Press Y and press ↵ Enter . This confirms that you want to save and exit the text editor. You will be returned to the standard command prompt in the Terminal.
Type source ~/.bashrc and press ↵ Enter . This loads the updated $PATH variable into your current session. [4] X Research source
Type the name of the program and press ↵ Enter . With the program’s directory now added to your $PATH variable, you should be able to launch the program by simply typing the program name and pressing Enter.
Installing and Uninstalling a Program in the Terminal
Press Ctrl + Alt + T to open the Terminal. In addition to launching programs from within the Terminal, you can also install and uninstall programs from within the Terminal. Use the keyboard shortcut to open the Terminal if you haven’t already done so.
Type sudo apt install [app_name] and press ↵ Enter (Debian). Replace «[app_name]» with the actual name of the program you want to install. This command works in Debian-based Linux distributions, such as Ubuntu, and Mint.
Type sudo apt remove [app_name] and press ↵ Enter (Debian). Replace «[app_name]» with the name of the app you want to uninstall. This command works in Debian-based Linux distributions, such as Ubuntu, and Mint.
Type sudo dnf install [app_name] and press ↵ Enter (Red Hat). Replace «[app_name]» with the actual name of the program you want to install. This command works for Red Hat Linux distributions such as RHEL, Fedora, and CentOS.
Type sudo dnf remove [app_name] and press ↵ Enter (Red Hat). Replace «[app_name]» with the name of the app you want to uninstall. This command works for Red Hat Linux distributions such as RHEL, Fedora, and CentOS. [5] X Research source
Type the name of the program and press ↵ Enter . Once the program is installed, you can launch the program by simply typing the program’s name and pressing Enter in the Terminal.
Expert Q&A
Tips
You Might Also Like
How to Change 4-Digit User Codes on Schlage Locks
How to Delay a Batch File: Timeout, Pause, Ping, Choice & Sleep
How to Factory Reset a Schlage Lock & Restore the Default Programming Code
Learn to Write Pseudocode: What It Is and Why You Need It
How to Start Coding: The Beginner’s Guide to Programming
3 Ways to Download GitHub Directories and Repositories
How To Execute a Command with a Shell Script in Linux
Shell is a command-line interpreter that allows the user to interact with the system. It is responsible for taking inputs from the user and displaying the output.
Shell scripts are a series of commands written in order of execution. These scripts can contain functions, loops, commands, and variables. Scripts are useful for simplifying a complex series of commands and repetitive tasks.
In this article, you will learn how to create and execute shell scripts for the command line in Linux.
Prerequisites
To complete this tutorial, you will need:
- Familiarity with using the terminal.
- Familiarity with a text editor.
- Familiarity with commands like chmod , mkdir , and cd .
Getting Started
A shell script needs to be saved with the extension .sh .
The file needs to begin with the shebang line ( #! ) to let the Linux system know which interpreter to use for the shell script.
For environments that support bash , use:
For environments that support shell , use:
This tutorial assumes that your environment supports bash .
Shell scripts can also have comments to increase readability. A good script always contains comments that help a reader understand exactly what the script is doing and the reasoning behind a design choice.
Creating and Running a Basic Shell Script
You can create a shell script using the vi editor, a cat command, or a text editor.
For this tutorial, you will learn about creating a shell script with vi :
This starts the vi editor and creates a basic_script.sh file.
Then, press i on the keyboard to start INSERT MODE . Add the following lines:
This script runs the commands whoami and date . whoami displays the active username. date displays the current system timestamp.
To save and exit the vi editor:
Finally, you can run the script with the following command:
You may get output that resembles the following:
Outputroot Fri Jun 19 16:59:48 UTC 2020
The first line of output corresponds to the whoami command. The second line of output corresponds to the date command.
You can also run a script without specifying bash :
Running the file this way might require the user to give permission first. Running it with bash doesn’t require this permission.
Output~bash: ./basic_script.sh: Permission denied
The command bash filename only requires the read permission from the file.
Whereas the command ./ filename , runs the file as an executable and requires the execute permission.
To execute the script, you will need to update the permissions.
This command applies chmod and gives x (executable) permissions to the current user.
Using Variables in Shell Scripts
Scripts can include user-defined variables. In fact, as scripts get larger in size, it is essential to have variables that are clearly defined and that have self-descriptive names.
Add the following lines to the script:
#!/bin/bash # This is a comment # defining a variable GREETINGS="Hello! How are you" echo $GREETINGS
GREETINGS is the variable defined and later accessed using $ (dollar sign symbol. There should be no space in the line where variables are being assigned a value.
This prints out the value assigned to the variable:
When the script is run, GREETINGS is defined and accessed.
Reading Input from the Command Line
Shell scripts can be made interactive with the ability to accept input from the command line. You can use the read command to store the command line input in a variable.
Add the following lines to the script:
#!/bin/bash # This is a comment # defining a variable echo "What is your name?" # reading input read NAME # defining a variable GREETINGS="Hello! How are you" echo $NAME $GREETINGS
A variable NAME has been used to accept input from the command line. This script waits for the user to provide input for NAME . Then it prints NAME and GREETINGS .
OutputWhat is your name? Sammy Sammy Hello! How are you
In this example, the user has provided the prompt with the name: Sammy .
Defining Functions
Users can define their own functions in a script. These functions can take multiple arguments.
Add the following lines to the script:
#!/bin/bash #This is a comment # defining a variable echo "What is the name of the directory you want to create?" # reading input read NAME echo "Creating $NAME . " mkcd () mkdir "$NAME" cd "$NAME" > mkcd echo "You are now in $NAME"
This script asks the user for a directory name. Then, it uses mkdir to create the directory and cd into it.
OutputWhat is the name of the directory you want to create? test_dir Creating test_dir . You are now in test_dir
In this example, the user has provided the prompt with the input: test_dir . Next, the script creates a new directory with that name. Finally, the script changes the user’s current working directory to test_dir .
Conclusion
In this article, you learned how to create and execute shell scripts for the command line in Linux.
Consider some repetitive or time-consuming tasks that you frequently perform that could benefit from a script.
Continue your learning with if-else , arrays, and arguments in the command line.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.