In bash, how do I pass the entire contents of a file to a program as a single string?












-1















I want to run a C program with the contents of a file as input (including newline characters), but the program only accepts a single string as input.



How do I pipe/redirect/pass the file contents into the program as a single string?



cat "$filename" > ./program_name doesn't preserve the content as a single string, and most other solutions seem to pass the command as the literal text of the command.










share|improve this question


















  • 1





    While not solving your immediate issue, is there any reason you are using command line arguments instead of reading standard input?

    – Mad Physicist
    Nov 14 '18 at 5:09






  • 1





    Why don't you read file in program rather than passing it as an argument. Let's say your file location is dynamic you could pass it then. Think about that if file is very big then it may cause slowness to your program.

    – RavinderSingh13
    Nov 14 '18 at 5:10













  • Those are both great suggestions, and I'll probably implement those changes soon. I only posted this question to share the fix I found in case anyone else was in a situation where they couldn't modify the program in question. I'd definitely advocate posting those as answers too!

    – Daniel
    Nov 14 '18 at 5:26
















-1















I want to run a C program with the contents of a file as input (including newline characters), but the program only accepts a single string as input.



How do I pipe/redirect/pass the file contents into the program as a single string?



cat "$filename" > ./program_name doesn't preserve the content as a single string, and most other solutions seem to pass the command as the literal text of the command.










share|improve this question


















  • 1





    While not solving your immediate issue, is there any reason you are using command line arguments instead of reading standard input?

    – Mad Physicist
    Nov 14 '18 at 5:09






  • 1





    Why don't you read file in program rather than passing it as an argument. Let's say your file location is dynamic you could pass it then. Think about that if file is very big then it may cause slowness to your program.

    – RavinderSingh13
    Nov 14 '18 at 5:10













  • Those are both great suggestions, and I'll probably implement those changes soon. I only posted this question to share the fix I found in case anyone else was in a situation where they couldn't modify the program in question. I'd definitely advocate posting those as answers too!

    – Daniel
    Nov 14 '18 at 5:26














-1












-1








-1








I want to run a C program with the contents of a file as input (including newline characters), but the program only accepts a single string as input.



How do I pipe/redirect/pass the file contents into the program as a single string?



cat "$filename" > ./program_name doesn't preserve the content as a single string, and most other solutions seem to pass the command as the literal text of the command.










share|improve this question














I want to run a C program with the contents of a file as input (including newline characters), but the program only accepts a single string as input.



How do I pipe/redirect/pass the file contents into the program as a single string?



cat "$filename" > ./program_name doesn't preserve the content as a single string, and most other solutions seem to pass the command as the literal text of the command.







string bash input pipe io-redirection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 4:48









DanielDaniel

927




927








  • 1





    While not solving your immediate issue, is there any reason you are using command line arguments instead of reading standard input?

    – Mad Physicist
    Nov 14 '18 at 5:09






  • 1





    Why don't you read file in program rather than passing it as an argument. Let's say your file location is dynamic you could pass it then. Think about that if file is very big then it may cause slowness to your program.

    – RavinderSingh13
    Nov 14 '18 at 5:10













  • Those are both great suggestions, and I'll probably implement those changes soon. I only posted this question to share the fix I found in case anyone else was in a situation where they couldn't modify the program in question. I'd definitely advocate posting those as answers too!

    – Daniel
    Nov 14 '18 at 5:26














  • 1





    While not solving your immediate issue, is there any reason you are using command line arguments instead of reading standard input?

    – Mad Physicist
    Nov 14 '18 at 5:09






  • 1





    Why don't you read file in program rather than passing it as an argument. Let's say your file location is dynamic you could pass it then. Think about that if file is very big then it may cause slowness to your program.

    – RavinderSingh13
    Nov 14 '18 at 5:10













  • Those are both great suggestions, and I'll probably implement those changes soon. I only posted this question to share the fix I found in case anyone else was in a situation where they couldn't modify the program in question. I'd definitely advocate posting those as answers too!

    – Daniel
    Nov 14 '18 at 5:26








1




1





While not solving your immediate issue, is there any reason you are using command line arguments instead of reading standard input?

– Mad Physicist
Nov 14 '18 at 5:09





While not solving your immediate issue, is there any reason you are using command line arguments instead of reading standard input?

– Mad Physicist
Nov 14 '18 at 5:09




1




1





Why don't you read file in program rather than passing it as an argument. Let's say your file location is dynamic you could pass it then. Think about that if file is very big then it may cause slowness to your program.

– RavinderSingh13
Nov 14 '18 at 5:10







Why don't you read file in program rather than passing it as an argument. Let's say your file location is dynamic you could pass it then. Think about that if file is very big then it may cause slowness to your program.

– RavinderSingh13
Nov 14 '18 at 5:10















Those are both great suggestions, and I'll probably implement those changes soon. I only posted this question to share the fix I found in case anyone else was in a situation where they couldn't modify the program in question. I'd definitely advocate posting those as answers too!

– Daniel
Nov 14 '18 at 5:26





Those are both great suggestions, and I'll probably implement those changes soon. I only posted this question to share the fix I found in case anyone else was in a situation where they couldn't modify the program in question. I'd definitely advocate posting those as answers too!

– Daniel
Nov 14 '18 at 5:26












1 Answer
1






active

oldest

votes


















2














./program_name "$(< "$filename")" should do the trick.



$(), similar to `` runs its contents as a sub-process and returns the result, so $(< "$filename") will redirect the contents of the file as the output of the sub-process.



"$(<"$filename")" does the same thing, except since it's wrapped in quotes, bash will not perform word splitting






share|improve this answer





















  • 1





    Bash allows you to use $(< "$filename"), which is a little bit faster than using cat. Also, the key mechanic that quotes suppress in this case is "word splitting".

    – Benjamin W.
    Nov 14 '18 at 5:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293346%2fin-bash-how-do-i-pass-the-entire-contents-of-a-file-to-a-program-as-a-single-st%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














./program_name "$(< "$filename")" should do the trick.



$(), similar to `` runs its contents as a sub-process and returns the result, so $(< "$filename") will redirect the contents of the file as the output of the sub-process.



"$(<"$filename")" does the same thing, except since it's wrapped in quotes, bash will not perform word splitting






share|improve this answer





















  • 1





    Bash allows you to use $(< "$filename"), which is a little bit faster than using cat. Also, the key mechanic that quotes suppress in this case is "word splitting".

    – Benjamin W.
    Nov 14 '18 at 5:52
















2














./program_name "$(< "$filename")" should do the trick.



$(), similar to `` runs its contents as a sub-process and returns the result, so $(< "$filename") will redirect the contents of the file as the output of the sub-process.



"$(<"$filename")" does the same thing, except since it's wrapped in quotes, bash will not perform word splitting






share|improve this answer





















  • 1





    Bash allows you to use $(< "$filename"), which is a little bit faster than using cat. Also, the key mechanic that quotes suppress in this case is "word splitting".

    – Benjamin W.
    Nov 14 '18 at 5:52














2












2








2







./program_name "$(< "$filename")" should do the trick.



$(), similar to `` runs its contents as a sub-process and returns the result, so $(< "$filename") will redirect the contents of the file as the output of the sub-process.



"$(<"$filename")" does the same thing, except since it's wrapped in quotes, bash will not perform word splitting






share|improve this answer















./program_name "$(< "$filename")" should do the trick.



$(), similar to `` runs its contents as a sub-process and returns the result, so $(< "$filename") will redirect the contents of the file as the output of the sub-process.



"$(<"$filename")" does the same thing, except since it's wrapped in quotes, bash will not perform word splitting







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 7:07

























answered Nov 14 '18 at 4:48









DanielDaniel

927




927








  • 1





    Bash allows you to use $(< "$filename"), which is a little bit faster than using cat. Also, the key mechanic that quotes suppress in this case is "word splitting".

    – Benjamin W.
    Nov 14 '18 at 5:52














  • 1





    Bash allows you to use $(< "$filename"), which is a little bit faster than using cat. Also, the key mechanic that quotes suppress in this case is "word splitting".

    – Benjamin W.
    Nov 14 '18 at 5:52








1




1





Bash allows you to use $(< "$filename"), which is a little bit faster than using cat. Also, the key mechanic that quotes suppress in this case is "word splitting".

– Benjamin W.
Nov 14 '18 at 5:52





Bash allows you to use $(< "$filename"), which is a little bit faster than using cat. Also, the key mechanic that quotes suppress in this case is "word splitting".

– Benjamin W.
Nov 14 '18 at 5:52


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293346%2fin-bash-how-do-i-pass-the-entire-contents-of-a-file-to-a-program-as-a-single-st%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine