Situatie
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.
Solutie
Running a Program From the Terminal
Type the name of the program and press ↵ Enter. Unlike the Windows command-line (CMD), you do not need to type out the full directory path or change directories for most programs in Linux. As long as a program is in your user “$PATH” variable, Linux will search $PATH for the program and launch it automatically.
- 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
- 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
- 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.
- 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.
Adding a Directory to your $PATH Variable
Type the command to export the programs directory to the $PATH variable.To do so, type export PATH=$PATH:[path/to/program] and press Enter. Replace “[path/to/program]” with actual directory tree the program is installed in. This will only last until the end of your current session.
- 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.
- Press Ctrl+X. This exits the text editor. It will ask if you want to save the file.
- 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.
- 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.
Leave A Comment?