How to compile c in terminal
How to compile c in terminal
How to create, compile & run a C Program in Linux terminal
The C programming language is still alive because it is simple and can do a lot of things. As we know Turbo C compiler is a discontinued integrated development environment, well, on Linux you don’t need it as there is already GNU Compiler Collection to compile and run C or C++ programs. Therefore, if you know the C language, it is much easier to learn, write programs and run other programming languages on Linux operating systems such as C ++, Java, Perl, or PHP, as the languages have certain similarities. Here we will show the steps to install GCC compiler and how to write, compile and run a C program in Linux.
Now, what is a program?
Let’s say you want to train your cat or puppy how to sit or jump on your commands, then what language you will use. Obviously, your mother tongue whether it is English, German, Chinese, Hindi, or something else. We will not bark or meaau. In a similar way, if we want to tell our computer to perform some specific tasks such as calculations, we need to train it how to do that, with the help of a set of rules. But the problem is Computer only understands the binary language that is 0 or 1, thus we have created Programming languages to create programs that we can understand and after compiling our computer and later can execute the same. Thus, a program is a sequence of actions to achieve a goal.
Steps to write, run and compile C program in Linux
Here we are using Ubuntu 20.04 LTS, however, the steps given here are not only for it. You can also implement on older Ubuntu versions such as 18.04/16.04 including Linux Mint, Debian, Kali, CentOS, RedHat, Fedora Elementary, and more…
1. Install Compiler and other Dev tools
To write and execute a C program on Linux, we need a compiler that will compile the code we have written and give us an executable file for the same. Therefore, for that, if you are on Debian or Ubuntu then install build-essential, and on RHEL based distros go for Development Tools.
For RHEL/Fedora/CentOS
Run system update command, first:
On Ubuntu or Debian systems
You should have sudo or root user access to run the above commands…
2. Check GCC version
GCC is the open-source library, which is an acronym for GNU Compiler Collection available for Linux and acts as a compiling system for Program C and other various programming languages. However, it mainly used to compile C and C++ programs… Thus, after installing the development tools in the first steps, you will also get GCC on your system. To confirm and check its version run:
To get full info:
To know only installed path
For version, only-
3. Open a Text editor on Ubuntu or RHEL
All Linux distros now come with graphical text editors, however, we can use the command terminal to create a text file to write C program codes as well using command line text editors such as nano, VIM, gedit, and more... Here we are using nano.
To install nano, if it is not on your system type:
RHEL/CentOS- sudo yum install nano
Ubuntu/Debian– sudo apt install nano
4. Write your first C Program in Linux terminal
Let’s create a demo C program in which we will include common C program library stdio.h to use various variables including functions for performing input and output. If you are learning C programming then you already familiar with the C libraries that we define in the header of the program to call various functions for performing various tasks.
For example, if you are writing a C program that mathematical functions then we need to declare math.h library, for graphics, we include graphics.h and so on…
Create a file:
Now, let’s add the following lines to create a simple C program that will give an output “Say hello to H2s” when we compile and execute it. To Save the file by pressing Ctrl+X, type Y, and then hit the Enter Key.
The explanation for the above command- In the above command first we have added stdio.h library in the header and then int main(); in this syntax main() is the entry point of any C/C++ program, in short, it tells the compiler to start compiling from this point and int main() function will accept any number of arguments but it returns integer value usually zero. However, that is an old way, developers usually prefer int main(void) this represents a function that expects no arguments or when a program doesn’t require any initial parameters.
After that we use printf function to show or print a text ” Say hello to H2S” and to break the output line or to have a page break, we used \n (Escape Sequences). As we have used the main () function that generally supposed to return 0 value to confirm there is no failure in compiling and the task has been finished successfully, however, if it gets non-zero that means a failure. Nevertheless, declaring return 0; is not compulsory even without the program and the main () function will work in the same way.
4. Compile with GCC
Now, let’s Compile our first C program, we have written. We use GCC compiler
In the above command, we are compiling the demo.c file or program using GCC and saving it in an executable format that is a demo. You can save it with some other name as well.
5. Run C program in Ubuntu Terminal
Let’s Run it. For that simply type:
In the above, example, we have compiled a C program demo.c and saved it an executable called demo. Thus, to run the same, we type:
Output:
6. C program to add numbers in Linux
Let’s go a little step further and write a simple program in C language to add numbers. In it, we will ask to users add two numbers and the program perform the addition and print the result.
Create a new program, let’s say sum.
Add the following code in that and save the file using Ctrl+X, type Y, and then press Enter key.
The explanation for the above code:
With the help int which is an integer variable, we are declaring three variables num1, num2, and sum. The num1 and num2 variables will hold the two numbers that a user will enter to get a result that will store in the sum variable.
int num1, num2, sum;
After that, we are printing a text to ask a user to enter two numbers that you want to add.
printf(«Enter two integers: «);
Then we will use scanf function to take inputs from the user in integer format using another variable %d or % i. %d specifies the type of variable as decimal and %i specifies the type as an integer.
Now, we will use + operator to add num1 and num2 and store the result of the same in sum variable:
sum = number1 + number2;
Finally, print the result of the addition using printf function. Now, what happening here is, first %i read the value stored in num1 and second %i read the value stored in num2, and third %i will read the value stored in sum. \n is just to page break line. After that, it prints all of them together which appears to the frontend user a familiar way to get an answer to its addition query.
printf(«%i + %i = %i \n», num1, num2, sum);
Compile and run your code
Closing thoughts:
Well, this tutorial is just an introduction on how to start with the C program in Linux, however, to go further in deep you have to acquire more knowledge through books on how libraries, functions, variables, and operators work in it.
How to Run C/C++ Programs in Linux [Terminal & Eclipse]
Brief: This tutorial teaches you to run C and C++ programs in Linux terminal. It also show the steps to setup a C++ development environment in Ubuntu Linux using Eclipse IDE.
I have been requested more than once about writing an easy to follow tutorial to run C++ program in Linux. In this guide, I’ll show you:
Process is pretty much similar to running C program in Linux.
Do note that I am using Ubuntu Linux while writing this article but same steps are valid for other Linux distributions based on Ubuntu such as Linux Mint, elementary OS etc.
Learn C++ for Free
If you are new to C++, join this FREE online course from Microsoft and learn C++ programming from the experts.
Prerequisite: Install build-essential
If you want to do coding in Ubuntu Linux, you must install build-essential package. It consists of various software that you will need to compile programs, including gcc and g++ compilers.
Normally, build-essential should already be installed on your system. But to make it sure, run the command below:
Method 1: Compile and run C++ program in Linux terminal
Once you have the build-essential installed, you are ready to code in C++. I believe that you already know how to code in C++, even a little bit. Our main aim is to see how to compile and run C++ programs in terminal.
Let’s take an example of Swap program C++ which I wrote in a file named swap.cpp. The content of this file is the following:
You can save the program wherever you want.
Compile C++ code in Linux terminal
To compile the program, go to the directory where you have saved the cpp file and use the command in the following format:
Run C++ code in Linux terminal
Once you have compiled the code, you’ll get the executable file. You just need to run it in the following manner:
This will run your code.
You can refer to this gif for a better demonstration of running a C++ program in Ubuntu Linux.
Method 2: Setup Eclipse for C++ programming in Ubuntu Linux
That was the basic way of running a C++ program in Linux. But if you are working on a C++ project, building and running individual files would be a nightmare.
This is where Integrated Development Environment (IDE) comes in picture. One can argue a lot about best IDE for Linux but if you ask for my advice, I’ll say go ahead with Eclipse. This is the best IDE for C++ development in my opinion. Did I mention that it is also open source?
Recommended Read:
How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner’s Tip]
Running C program in Linux command line is not that difficult. Running it in a code editor like Visual Studio Code is even easier. Learn both methods in this tutorial.
Install Eclipse in Ubuntu based Linux distributions
For Ubuntu Linux, you can simply click on the link below to install Eclipse from Ubuntu Software Center.
Alternatively, you can install it using apt-get commands in the terminal:
Install Eclipse C++ Development Tooling (CDT) Plugin
Once you have it installed, it is time to prepare it for C++ development. By default, Eclipse is configured for Java development.
To configure it for C++ development, we need to install a plugin called C++ Development Tooling (CDT). To install CDT:
Step 1:
In the Eclipse menu, go to Help and then select Install New Software.
Step 2:
Next, click on the “Available Software Sites” link.
Step 3:
In the next step, search for CDT and check the box to select it for installation. Click OK afterward.
Step 4:
In here, select the newly added source from the drop down. It will now show you the option for C++ CDT. Just select C++ Development Tools here.
A few click on the Next button.
Accept the terms of course.
It will get the software from the repository and install it.
Once the installation is finished, you need to restart Eclipse.
Compile and run C++ program with Eclipse CDT
You’ll see the information about C++ Plugin at the next start.
You can now import or create C++ projects.
Once you have everything ready, you can compile the C++ project and run it:
That’s all you need to get started with C++ development in Ubuntu Linux. I hope you found this article useful. Questions and suggestions are welcomed.
Creator of It’s FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries ranging from Agatha Christie and Sherlock Holmes to Detective Columbo & Ellery Queen. Also a movie buff with a soft corner for film noir.
Similar Posts
Fedora 26 Is Released! Check Out The New Features
Check out the new features in the newly released Fedora 26.
Restore Android Factory Image In Nexus 7 2013 In Linux
The other day I installed Ubuntu Touch in Nexus 7 2013. Since it’s quite unstable and I was kind of missing Android games, I decided to restore Android back in Nexus 7 2013. I plan to dual boot Android and Ubuntu Touch, may be sometime soon. In this tutorial we shall see how to restore…
How to Make a Transparent Background in GIMP [Step by Step Guide]
Learn how to remove the background of an image and make it transparent using the free and open source Image editor GIMP.
50 Best Ubuntu Apps You Should Be Using Right Now
Looking for best Ubuntu apps? Look no further than this comprehensive list of more than 50 essential Ubuntu applications that would improve your Ubuntu experience.
Ubuntu Linux Spotted in Real Life
Here’s a collection of Ubuntu spotted in the wild pictures for die hard Ubuntu fans.
How to Install VLC on Fedora Linux
Here’s a simple and quick tutorial showing the necessary steps for installing VLC on Fedora Linux.
An error is occuring.
‘__max’ is not a member of ‘__gnu_cxx::__numeric_traits’
Hello! Although I have practiced bash shell scripts, now I want to try C programming.
Then all these instructions could be used to the “C”? I have no programming experience, so I have ever not understood the difference C and C++.
I wish I could get any advice to start learning C programming. Thank you for reading.
That’s right. You use gcc instead of g++ like this:
I am writing a new tutorial about it.
Thank you for your reply. Compiling my study code is successfully done. I am waiting for the new tutorial!
An error has occurred. See the log file
/home/anshul/.eclipse/org.eclipse.platform_3.8_155965261/configuration/1592107262412.log
what to do? please tell.
Check the log files. What error does it show.
You ara a great man for doing this tutorial, thanks for your time! Namaste
Glad you liked it, Franco 🙂
Hello all,
Can someone tell me how to compile bunch of C++ source files in Eclipse IDE Ubuntu 16.04 LTS
Don’t find what you are looking for?
Well done, Sherlock!
Your sharp observation skill and intellect have identified a potential issue with this article.
Is it a grammatical mistake or a simple typo? That happens from time to time.
Is there some incorrect technical information? It’s possible that we were not clear on the topic.
Part of the article contains outdated steps or commands? We have over 1500 articles in the last ten years. It’s possible that some articles that worked well five years ago won’t work today.
Is there an issue with the UI and UX of the website? Some button not working? Link leading to a dead page? Or any other issue with the website elements?
Dear Holmes, help your Watson (that’s us) by explaining the details.
Learn and Try
«It does not matter where you go and what you study, what matters most is what you share with yourself and the world.»
Run a C/C++ program on terminal using gcc compiler
Follow these steps to run programs on terminal:
Step 1. Open terminal.
Step 2. Type command to install gcc or g++ complier:
$ sudo apt-get install build-essential
This will install the necessary C/C++ development libraries for your Ubuntu to create C/C++ programs.
To check gcc version type this command:
$ gcc –version or gcc –v
Step 3. Now go to that folder where you will create C/C++ programs. I am creating my programs in Documents directory. Type these commands:
$ cd Documents/
$ sudo mkdir programs
$ cd programs/
Step 4. Open a file using any editor.
$ sudo gedit first.c (for C programs)
$ sudo gedit hello.cpp (for C++ prgrams)
Step 5. Add this code in the file:
(i). C program code:
printf(“\n\nWelcome to my Homepage!!\n\n”);
(i). C++ program code:
using namespace std;
Step 6. Save the file and exit.
Step 7. Compile the program using any of the following command:
(i). Compiling C program.
$ sudo gcc first.c
It will create an executable file with “.out” extension named as “a.out”.
$ sudo gcc –o first first.c
Where first is the executable or object file of first.c program.
(ii). Compiling C++ program.
$ sudo g++ hello.cpp (or)
[Note: Make sure you are in the same directory where you have created your program before compiling it.]
Step 8. To run this program type this command:
(i). For running C program
(ii). For running C++ program
How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner’s Tip]
How do you program in C on Linux? It is indeed very easy and consists of three simple steps.
Step 2: You compile the program and generate the object file using gcc compiler in a terminal like this:
Step 3: You run the generated object file to run your C program in Linux:
» data-medium-file=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-linux-300×151.png» data-large-file=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-linux.png» width=»795″ height=»399″ src=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-linux.png» alt=»Running C Program Linux» data-lazy-srcset=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-linux.png 795w, https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-linux-300×151.png 300w, https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-linux-768×385.png 768w, https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-linux-150×75.png 150w» data-lazy-sizes=»(max-width: 795px) 100vw, 795px» data-lazy-src=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-linux.png?is-pending-load=1″ srcset=»data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″>
This was just the quick summary on how to compile and run C program in Linux. If you are new to either C or Linux, I’ll show these steps in detail so that you feel comfortable coding C program in Linux environment.
In fact, I’ll discuss how to run C programs in Linux terminal as well as in code editor.
Method 1: How to run C programs in Linux terminal
In order to run a C program in Linux, you need to have a C compiler present on your systems. The most popular compiler is gcc (GNU Compiler Collection).
You can install gcc using your distribution’s package manager. In Debian and Ubuntu-based Linux distributions, use the apt command:
Switch to directory where you have kept your C program (or provide the path) and then generate the object file by compiling the program:
Keep in mind that it is optional to provide the output object file (-o my_program). If you won’t do that, an object file named a.out will be automatically generated. But this is not good because it will be overwritten for each C program and you won’t be able to know which program the a.out object file belongs to.
Once you have your object file generated, run it to run the C program. It is already executable. Simple use it like this:
And it will display the desired output, if your program is correct. As you can see, this is not very different from running C++ programs in Linux.
Every time you make a change in your program, you have to compile it first and then run the generated object file to run the C program.
Method 2: How to run C programs in Linux using a code editor like Visual Studio Code
Not everyone is comfortable with command line and terminal and I totally understand that.
You can use a proper C/C++ IDE like Eclipse or Code Blocks but they are often too heavy programs and more suitable for large projects.
I recommend using an open source code editor like Visual Studio Code or Atom. These are basically text editors and you can install add-ons to compile and run programs directly from the graphical code editor.
I am using Visual Studio Code editor in this example. It’s a hugely popular open source code editor from Microsoft.
First thing first, install Visual Studio Code in Ubuntu from the software center. For other distributions, please check your Linux distribution’s package manager or software center. You may also check the official website for more information.
Start Visual Studio Code and open/create a project and create your C program here. I am using a sample Hello World program.
» data-medium-file=»https://itsfoss.com/wp-content/uploads/2020/11/c-program-visual-studio-code-linux-300×165.png» data-large-file=»https://itsfoss.com/wp-content/uploads/2020/11/c-program-visual-studio-code-linux-800×441.png» width=»800″ height=»441″ src=»https://itsfoss.com/wp-content/uploads/2020/11/c-program-visual-studio-code-linux-800×441.png» alt=»C Program Visual Studio Code Linux» data-lazy-srcset=»https://itsfoss.com/wp-content/uploads/2020/11/c-program-visual-studio-code-linux-800×441.png 800w, https://itsfoss.com/wp-content/uploads/2020/11/c-program-visual-studio-code-linux-300×165.png 300w, https://itsfoss.com/wp-content/uploads/2020/11/c-program-visual-studio-code-linux-768×423.png 768w, https://itsfoss.com/wp-content/uploads/2020/11/c-program-visual-studio-code-linux-150×83.png 150w, https://itsfoss.com/wp-content/uploads/2020/11/c-program-visual-studio-code-linux.png 811w» data-lazy-sizes=»(max-width: 800px) 100vw, 800px» data-lazy-src=»https://itsfoss.com/wp-content/uploads/2020/11/c-program-visual-studio-code-linux-800×441.png?is-pending-load=1″ srcset=»data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″>
You must ensure that you have gcc compiler installed on your Linux system.
Next thing you would want is to use an extension that allows you to run the C code. Microsoft may prompt you for installing its own extension for C/C++ program but it is complicated to setup and hence I won’t recommend it.
Instead, I suggest using the Code Runner extension. It’s a no-nonsense extension and you can run C and C++ code easily without additional configuration.
Go to the Extensions tab and search for ‘Code Runner’ and install it.
» data-medium-file=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code-300×188.png» data-large-file=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.png» width=»800″ height=»500″ src=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.png» alt=»Running C Program In Linux With Visual Studio Code» data-lazy-srcset=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.png 800w, https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code-300×188.png 300w, https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code-768×480.png 768w, https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code-150×94.png 150w» data-lazy-sizes=»(max-width: 800px) 100vw, 800px» data-lazy-src=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.png?is-pending-load=1″ srcset=»data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″> Install Code Runner extension for running C/C++ program
Restart Visual Studio Code. Now, you should be able to run the C code by using one of the following way:
» data-medium-file=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code-300×188.jpg» data-large-file=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.jpg» width=»800″ height=»500″ src=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.jpg» alt=»Running C Program In Linux With Visual Studio Code» data-lazy-srcset=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.jpg 800w, https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code-300×188.jpg 300w, https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code-768×480.jpg 768w, https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code-150×94.jpg 150w» data-lazy-sizes=»(max-width: 800px) 100vw, 800px» data-lazy-src=»https://itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.jpg?is-pending-load=1″ srcset=»data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″> Right click the program file and choose Run Code
When you run the program, it is compiled automatically and then run. You can see the output in terminal that is opened at the bottom of the editor. What could be better than this?
» data-medium-file=»https://itsfoss.com/wp-content/uploads/2020/11/run-c-program-in-linux-with-visual-studio-code-300×188.jpg» data-large-file=»https://itsfoss.com/wp-content/uploads/2020/11/run-c-program-in-linux-with-visual-studio-code.jpg» width=»800″ height=»500″ src=»https://itsfoss.com/wp-content/uploads/2020/11/run-c-program-in-linux-with-visual-studio-code.jpg» alt=»Run C Program In Linux With Visual Studio Code» data-lazy-srcset=»https://itsfoss.com/wp-content/uploads/2020/11/run-c-program-in-linux-with-visual-studio-code.jpg 800w, https://itsfoss.com/wp-content/uploads/2020/11/run-c-program-in-linux-with-visual-studio-code-300×188.jpg 300w, https://itsfoss.com/wp-content/uploads/2020/11/run-c-program-in-linux-with-visual-studio-code-768×480.jpg 768w, https://itsfoss.com/wp-content/uploads/2020/11/run-c-program-in-linux-with-visual-studio-code-150×94.jpg 150w» data-lazy-sizes=»(max-width: 800px) 100vw, 800px» data-lazy-src=»https://itsfoss.com/wp-content/uploads/2020/11/run-c-program-in-linux-with-visual-studio-code.jpg?is-pending-load=1″ srcset=»data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″> Program output is displayed in the bottom section of the editor
Which method do you prefer?
Running a few C programs in Linux command line is okay but using a code editor is much easier and saves time. Won’t you agree?
I let you decide whichever method you want to use.
Creator of It’s FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries ranging from Agatha Christie and Sherlock Holmes to Detective Columbo & Ellery Queen. Also a movie buff with a soft corner for film noir.
How can I compile and run C/C++ code in a Unix console or Mac terminal?
How can I compile/run C or C++ code in a Unix console or a Mac terminal?
19 Answers 19
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
If it is a simple single-source program,
where the source file is foo.c, foo.cpp, etc., you don’t even need a makefile. Make has enough built-in rules to build your source file into an executable of the same name, minus the extension.
So to run the built executable foo :
This is the command that works on all Unix machines. I use it on Linux/Ubuntu, but it works in OS X as well. Type the following command in Terminal.app.
-o is the letter O, not zero
lab21 will be your executable file
iterative.cpp is your C++ file
After you run that command, type the following in the terminal to run your program:
Two steps for me:
All application execution in a Unix (Linux, Mac OS X, AIX, etc.) environment depends on the executable search path.
You can display this path in the terminal with this command:
On Mac OS X (by default) this will display the following colon separated search path:
So any executable in the listed directories can by run just by typing in their name. For example:
This runs /bin/cat and displays mytextfile.txt to the terminal.
To run any other command that is not in the executable search path requires that you qualify the path to the executable. So say I had an executable called MyProgram in my home directory on Mac OS X I can fully qualify it like so:
If you are in a location that is near the program you wished to execute you can qualify the name with a partial path. For example, if MyProgram was in the directory /Users/oliver/MyProject I and I was in my home directory I can qualify the executable name like this, and have it execute:
Or say I was in the directory /Users/oliver/MyProject2 and I wanted to execute /Users/oliver/MyProject/MyProgram I can use a relative path like this, to execute it:
Similarly if I am in the same directory as MyProgram I need to use a «current directory» relative path. The current directory you are in is the period character followed by a slash. For example:
To determine which directory you are currently in use the pwd command.
If you are commonly putting programs in a place on your hard disk that you wish to run without having to qualify their names. For example, if you have a «bin» directory in your home directory for regularly used shell scripts of other programs it may be wise to alter your executable search path.
) character is being used as a shortcut for /Users/oliver. Also note that the hash bang (#!) line needs to be the first line of the file (if it doesn’t already exist). Note also that this technique requires that your login shell be bash (the default on Mac OS X and most Linux distributions). Also note that if you want your programs installed in
/bin to be used in preference to system executables your should reorder the export statement as follows: