# Putting bash script in C program

**URL:** <https://community.unix.com/t/putting-bash-script-in-c-program/324437>\
**Category:** Programming\
**Created:** [February 16, 2013, 11:08pm UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437 "2013-02-16T23:08:31Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [February 16, 2013, 11:08pm UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/1 "2013-02-16T23:08:31Z")

</div>

suppose i have a bash script:

```nohighlight
#!/bin/bash

echo "hello"
echo "how are you"
echo "today"

```

how can i put the entire script above into a basic c program?

i do not want to translate the bash code to a c code. i want C to run the bash code. is this possible?

i found this on the internet but i'm not sure i understand it:

```nohighlight
#include <stdio.h>
#include <stdlib.h>
 
#define SHELLSCRIPT "\
for ((i=0 ; i < 10 ; i++))\n\
do\n\
echo \"Count: $i\"\n\
done\n\
"
 
int main()
{
    puts("Will execute sh with the following script :");
    puts(SHELLSCRIPT);
    puts("Starting now:");
    system(SHELLSCRIPT);
    return 0;
}

```

i'm looking for a C program where i can just insert the bash code into it and have it run. something like this:

```nohighlight
#include <stdio.h>
#include <stdlib.h>

int main()
{
    puts("Will execute sh with the following script :");
    puts(#!/bin/bash
    echo "hello"
    echo "how are you"
    echo "today");
    puts("Starting now:");
    system(SHELLSCRIPT);
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [February 16, 2013, 11:44pm UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/2 "2013-02-16T23:44:20Z")

</div>

```nohighlight
#include <stdio.h>
#include <stdlib.h>

#define SHELLSCRIPT "\
#/bin/bash \n\
echo \"hello\" \n\
echo \"how are you\" \n\
echo \"today\" \n\
"

int main()
{
    puts("Will execute sh with the following script :");
    puts(SHELLSCRIPT);
    puts("Starting now:");
    system(SHELLSCRIPT);
    return 0;
}

```

The `#define SHELLSCRIPT` directive is used in C to define a named constant: `SHELLSCRIPT` which contains the shell script.

The back slash `\` at the end of each line is used to type the code in next line for better readability.

The new line `\n` is used to put the code in next line.

Double quotes `"` within the shell script has to be escaped `\"` to distinguish it between the original opening and closing double quotes of constant value.

Finally the `system` function is called by passing the named constant: `SHELLSCRIPT` as argument which contains the shell script.

I hope it makes sense now.

---

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [February 16, 2013, 11:51pm UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/3 "2013-02-16T23:51:19Z")

</div>

> [@bipinajith](#):
>
> ```plaintext
> #include <stdio.h>
> #include <stdlib.h>
> 
> #define SHELLSCRIPT "\
> #/bin/bash \n\
> echo \"hello\" \n\
> echo \"how are you\" \n\
> echo \"today\" \n\
> "
> 
> int main()
> {
> puts("Will execute sh with the following script :");
> puts(SHELLSCRIPT);
> puts("Starting now:");
> system(SHELLSCRIPT);
> return 0;
> }
> 
> ```
> 
> The `#define SHELLSCRIPT` directive is used in C to define a named constant: `SHELLSCRIPT` which contains the shell script.
> 
> The back slash `\` at the end of each line is used to type the code in next line for better readability.
> 
> The new line `\n` is used to put the code in next line.
> 
> Double quotes `"` within the shell script has to be escaped `\"` to distinguish it between the original opening and closing double quotes of constant value.
> 
> Finally the `system` function is called by passing the named constant: `SHELLSCRIPT` as argument which contains the shell script.
> 
> I hope it makes sense now.

thank you soo much. you are a godsend. thank you.

i'm curious though, and please forgive my newbie status to c++, but can you please tell me how do i run this program? i like to keep things simple.

so if i named your script to mycplus, can i run it from the command line like this:

```plaintext
#mycplus

```

can a C program that contains a bash script like the one above be compiled and made into an executable?

how would you compile it?

thanks again!!!

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [February 17, 2013, 12:00am UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/4 "2013-02-17T00:00:49Z")

</div>

First of all this is a C Program. Below are the steps to compile:

1. Create a `C` program file:

```nohighlight
$ cat > cprog.c
#include <stdio.h>
#include <stdlib.h>

#define SHELLSCRIPT "\
#/bin/bash \n\
echo \"hello\" \n\
echo \"how are you\" \n\
echo \"today\" \n\
"

int main()
{
    puts("Will execute sh with the following script :");
    puts(SHELLSCRIPT);
    puts("Starting now:");
    system(SHELLSCRIPT);
    return 0;
}

```

1. Compile the `C` program file using `cc` compiler:

```nohighlight
$ cc cprog.c

```

1. Compiler will compile & link and create an output file: `a.out` which you can run to see the result:

```nohighlight
$ ./a.out

```

If you want to create an output file with a name of your choice, use `-o` option:

```nohighlight
$ cc cprog.c -o cprog

```

Run the compiled & linked output file created: `cprog`

```nohighlight
$ ./cprog

```

---

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [February 17, 2013, 12:07am UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/5 "2013-02-17T00:07:29Z")

</div>

thanks again. one last question.

the bash script i intend on embedding into this C program will be accepting several arguments from the command line. and it is a very big bash script. will C be able to catch the arguments?

meaning, after i have embedded the bash script into C, i will be running the C program from the command line like this:

```nohighlight
./cprog arg1 arg2 arg3 arg4 arg5 'arg6' 'arg7' arg8 arg9 arg10

```

the arguments are meant for the bash script. will C be able to pass it to the script?

if i was running the bash script alone without it being embedded into C, the bash script will accept and use the arguments just fine. but i just want to make sure when it is embedded in C there wont be problems.

thanks again.

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [February 17, 2013, 12:14am UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/6 "2013-02-17T00:14:19Z")

</div>

No, that will not work. To make things simple I would suggest to write the `bash` script in a separate file and call it from `C` program by passing the required arguments:

```nohighlight
#include <stdio.h>
#include <stdlib.h>

int main()
{
    puts("Starting now:");
    system("/path_to_script/script_name arg1 arg2 arg3");
    return 0;
}

```

This way it will be easy to maintain.

---

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [February 17, 2013, 12:32am UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/7 "2013-02-17T00:32:35Z")

</div>

> [@bipinajith](#):
>
> No, that will not work. To make things simple I would suggest to write the `bash` script in a separate file and call it from `C` program by passing the required arguments:
> 
> ```plaintext
> #include <stdio.h>
> #include <stdlib.h>
> 
> int main()
> {
> puts("Starting now:");
> system("/path_to_script/script_name arg1 arg2 arg3");
> return 0;
> }
> 
> ```
> 
> This way it will be easy to maintain.

that was my first option. but the whole goal here was to conceal the bash code. so i figure if i can embed it into C, i'll be able to hide the code. but if C is calling the script, the script will be open for everyone to read.

as i was typing this, something else occurred to me. i'm thinking, i can encode the script using a password. tell C to decode the script using the password i put in the C code. and then C can call the script after script has been decoded.

dont know if that makes sense.

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [February 17, 2013, 12:49am UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/8 "2013-02-17T00:49:21Z")

</div>

Yes, it makes sense, but did you consider using utilities like `SHC` - shell script compiler?

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 17, 2013, 1:03am UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/9 "2013-02-17T01:03:17Z")

</div>

> [@skysmart](#):
>
> but the whole goal here was to conceal the bash code.

Won't work.

`strings executablefile`

try shc.

---

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [February 17, 2013, 1:20am UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/10 "2013-02-17T01:20:35Z")

</div>

> [@corona688](#):
>
> Won't work.
> 
> `strings executablefile`
> 
> try shc.

i tried shc but the bash script i have is too big for shc.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 17, 2013, 1:29am UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/11 "2013-02-17T01:29:40Z")

</div>

Why is it so big? Have you embedded data in it?

---

<div class="post-metadata">

**Author:** ![achenle](https://community.unix.com/letter_avatar/achenle/32/6_449c533f586939b6e5f246632a46421f.png) [@achenle](https://community.unix.com/u/achenle)\
**Post date:** [February 17, 2013, 8:25am UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/12 "2013-02-17T08:25:00Z")

</div>

> [@skysmart](#):
>
> ... but the whole goal here was to conceal the bash code. ...

If you don't want anyone to know what your code does, you can't let them have any access at all to it.

If you distribute a binary to run on another's computer(s), anyone who really wants to is going to be able to figure out everything it does.

Although I will admit that there can be value in obfuscating what's going on, if only to prevent those just smart enough to be dangerous from damaging things....

---

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [February 17, 2013, 10:15am UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/13 "2013-02-17T10:15:57Z")

</div>

> [@corona688](#):
>
> Why is it so big? Have you embedded data in it?

yes. i just followed the instructions. my bash script is over 10MB in size. so when u run shc on it, it aborts complaining about the bytes.

i really believe this can be done. we just need to put heads together.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 17, 2013, 4:06pm UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/14 "2013-02-17T16:06:30Z")

</div>

> [@skysmart](#):
>
> yes.

It would be better to put the data in the C part. If you make it a global array, they can be quite large. You could write it into the script's standard input with popen.

```plaintext
const unsigned char bytes[]={ 0x00, 0xab, ... };

int main(void)
{
        FILE *fp=popen("programname", "w");
        fwrite(bytes, 1, sizeof(bytes), fp);
}

```

you might want a decryption step between that and writing it of course, since people may be peeking in your file.

> [@](#):
>
> i really believe this can be done.

By definition your program includes complete instructions for decrypting itself. How could you possibly hope to hide it? At best you can obfuscate it. A truly determined person will find the contents.

If you could force people to login to your sever to use it, that could be made secure. the program wouldn't need to run on their machine, they wouldn't have access to the file.

---

<div class="post-metadata">

**Author:** ![hanson44](https://community.unix.com/letter_avatar/hanson44/32/6_449c533f586939b6e5f246632a46421f.png) [@hanson44](https://community.unix.com/u/hanson44)\
**Post date:** [March 6, 2013, 3:27am UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/15 "2013-03-06T03:27:27Z")

</div>

The bash script is over 10 MB in size? I think this needs to be converted to a compiled program in C, etc. How can you possibly maintain a bash script that large?

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [March 6, 2013, 5:23pm UTC](https://community.unix.com/t/putting-bash-script-in-c-program/324437/16 "2013-03-06T17:23:06Z")

</div>

> [@hanson44](#):
>
> The bash script is over 10 MB in size? I think this needs to be converted to a compiled program in C, etc. How can you possibly maintain a bash script that large?

I think most of the 10mb is file contents, not script code. A big tarball that's been base64'ed into text or something. It's embedded in the script with the hopes of obscuring it.
