How To Run A Program In Linux

How To Run A Program In Linux

Reading time1 min
#Linux#CommandLine#Programming#Shell#Terminal

Mastering Command-Line Execution: How to Run Any Program in Linux Like a Pro

Forget the GUI crutches—true Linux power users harness the command line to run programs with precision and speed. This guide reveals how mastering program execution is your gateway to unlocking Linux's full potential.


If you're new to Linux or even a seasoned user, getting comfortable with running programs from the command line is essential. Why? Because the command line offers unmatched efficiency, flexibility, and control compared to graphical interfaces. Whether you’re a developer, sysadmin, or power user, effortlessly launching and managing programs in Linux can streamline your workflow and open doors to automation and troubleshooting.

In this post, we’ll cover the essentials: how the shell finds programs, executing them in various ways, passing arguments, running background jobs, and handling scripts. By the end, you'll run programs like a pro — no mouse required!


1. Understanding How Linux Runs Programs

At its core, when you type a command in the terminal and press Enter:

  • The shell (such as bash, zsh, or fish) parses your input.
  • It searches for an executable file matching the command name.
  • Once found, it executes that program with any given arguments.

How does the shell find executable files?

Linux uses an environment variable called $PATH. It’s a colon-separated list of directories where executables live. For example:

echo $PATH
# Output might be:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

When you type ls, your shell looks through each directory in $PATH/usr/bin, /bin, etc.— until it finds an executable named ls.

Tip: To see exactly which executable will run when typing a command:

which ls
# Output:
/bin/ls

2. Running Programs by Command Name

The simplest way to run a program is by typing its name:

firefox

This runs Firefox if it’s installed and located in one of the directories in your $PATH.

What if it’s not in your PATH?

If you try to run something like ./myprogram (a script or an executable in the current directory), you need to specify its path explicitly because . (the current directory) is usually not part of $PATH for security reasons.

Example:

./myprogram arg1 arg2

This runs myprogram located in the current directory with two arguments.


3. Running Scripts & Making Them Executable

If you have a script written in Bash, Python, or another language:

Step 1: Add executable permissions

chmod +x ./myscript.sh

Step 2: Run it

./myscript.sh

Make sure the script specifies its interpreter at the top (called shebang):

#!/bin/bash
echo "Hello from script!"

Or for Python:

#!/usr/bin/env python3
print("Running Python script!")

The shebang tells Linux which interpreter to use when executing this file.


4. Passing Arguments to Programs

Most programs accept parameters or options that modify their behavior.

Example:

grep "error" /var/log/syslog

Here, grep searches for the term “error” inside /var/log/syslog.

Arguments follow this pattern:

  • Flags/options usually start with one or two dashes (e.g., -h, --help)
  • Positional parameters follow options (e.g., filenames)

Example with options:

ls -l /etc

Lists files in /etc directory using long format (-l).


5. Running Programs in Background & Foreground

Sometimes you want to keep working even while a program runs.

Run process in background:

Add an ampersand (&) at the end:

gedit &

This launches Gedit text editor and immediately returns control to your shell so you can continue typing commands.

Bring background jobs back into foreground:

Use fg command

fg %1  # Brings job number 1 to foreground

List running jobs with:

jobs

6. Combining Commands & Running Programs Efficiently

You can chain commands for efficiency.

  • Sequential execution (wait for each)

    mkdir newfolder && cd newfolder && touch file.txt
    

    Each command runs only if previous succeeds.

  • Run multiple commands simultaneously

    ./longtask.sh & ./anothertask.sh &
    
  • Use piping (|)

    Example: List files and search output

    ls -l /var/log | grep syslog
    

7. Advanced Tip: Running Programs as Another User or with Elevated Privileges

Sometimes system tasks require root access.

  • Use sudo to run a command as root:

    sudo apt update
    
  • Run command as different user (username):

    sudo -u username somecommand
    

Make sure you have appropriate permissions!


Summary Checklist for Running Programs Like a Pro

TaskCommand Example
Run program by namefirefox
Run program from current dir./program_name
Make script executablechmod +x script.sh
Pass argumentsgrep "pattern" file.txt
Run process in backgroundgedit &
Bring background job foregroundfg %job_number
Check executable locationwhich program_name
Chain commands sequentiallycmd1 && cmd2 && cmd3

Mastering these fundamental skills will set you on track toward using Linux command-line tools with confidence and finesse. Start experimenting today—try launching common apps from your terminal and explore options within your favorite programs using commands like <command> --help. Soon enough, GUI reliance will feel limiting rather than liberating!

Happy hacking! 🚀


Got questions or want me to cover specific CLI tools? Drop a comment below!