doexec Command in Linux



The Linux doexec command runs an executable with an arbitrary argv[0]. The argv[0] is the argument array's first index indicating the executable's name.

A C or C++ programs main function can receive arguments from the command line. These arguments are passed to the program in the form of an array of strings called argv, which stands for argument vector. The first element of the argument vector indicates the name of the executable. The doexec command allows to run the executable with a different name.

Table of Contents

Here is a comprehensive guide to the options available with the doexec command −

Understanding argc and argv Parameters

To accept arguments from the command line, the argc and argv parameters are mentioned in the main function of the C or C++ program.

main (int argc, char *argv[])

The integer argc indicates the number of arguments the program can take, and argv is an argument array of strings. It is important to note that argc and argv names are traditionally used, these parameters can also be mentioned with other names.

The first element of the argument vector, argv[0] is the program name that is being executed from the command line, while argv[1] and argv[2] are arguments one and two. For example, the following C program takes arguments from the command line and prints their value to standard output.

#include <stdio.h>

int main(int argc, char *argv[]) {
   printf("Number of arguments: %d\n", argc);
   for(int i = 0; i < argc; i++) {
      printf("argv[%d]: %s\n", i, argv[i]);
   }
   return 0;
}
Understanding argc and argv Parameters

The output shows that argv[0] is the program name (test) being executed. The hello and world are argv[1] and argv[2] respectively.

Syntax of doexec Command

The syntax of using the Linux doexec command is as follows −

doexec [executable_path] argv[0] argv[1n]

Specify the executable path in the [executable_path] field and mention the executable arbitrary name in the argv[0] field. Other arguments can be mentioned in the argv[1...n] field.

Using doexec Command in Linux

This section demonstrates the usage of the doexec command in Linux.

Running an executable using the doexec command requires an executable file. To create an executable, first create a C program file using any text editor −

sudo nano test.c

Use the example program from the Understanding argc and argv Parameters section −

Using doexec Command 1

Save the program file and exit the editor by pressing ctrl+x and typing y.

The next step includes the compilation of the program. The C and C++ programs can be compiled using the gcc command. The gcc is a command line utility that can compile C, C++, Objective C, and various other programming languages.

Note − Before performing the following step, ensure gcc is installed using the gcc --version command. If it is not installed, install it using distribution-specific commands.

Using doexec Command 2

To compile a C program, use the command given below −

gcc test.c -o test

Here, test.c is the program file, and the -o flag is used to specify the output file name. It will generate the executable with the name of the test in the current working directory.

Using doexec Command 3

Note − If the output file name is not specified, the executable will be generated with the name of a.out.

Now, run the doexec command on the test executable −

doexec test myprogram arg1 arg2
Using doexec Command 4

The above command will change the value of argv[0] to the arbitrary name myprogram, as shown in the output.

Conclusion

The doexec command in Linux runs the executable with a specified name. It is not the standard command of Linux and may not be available on many Linux distributions.

The main purpose of running the executable with a different name is to test the program by executing it with another name or hiding the original name during the execution. Moreover, some systems might expect a certain value in the argv[0], in that case, the doexec command can be a handy tool.

In this tutorial, we explained the doexec command, its syntax, and its usage in Linux.

Advertisements