Write string to a file without a shell [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












Suppose I have an environment where there isn't a shell running, so I can't use redirection, pipes, here-documents or other shell-isms, but I can launch a command (through execvp or some similar way). I want to write an arbitrary string to a named file. Is there a standard command that will do something like:



somecommand outputfile 'string'


for instance:



somecommand /proc/sys/net/ipv4/ip_forward '1'


A really dumb example might be:



curl -o /proc/sys/net/ipv4/ip_forward http://example.com/1.txt


where I set up 1.txt to contain the string I want.



Is there a common command that can be abused to do this?







share|improve this question













closed as unclear what you're asking by Kusalananda, schily, Thomas, Jesse_b, Jeff Schaller Jul 28 at 18:38


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    This would never fly, total vector for hacker to exploit.
    – slm♦
    Jul 27 at 23:13










  • Why is writing to a file a vector for hackers? The file is written with the standard privileges of the calling user (so the /proc/sys/... examples would need root or sudo)
    – user1908704
    Jul 28 at 0:06







  • 2




    If you don't have a shell, what's going to run your command? I notice you've tagged the question with "scripting". Are you referring to some other scripting language than a shell scripting language (Perl? Python?). Please clarify the question.
    – Kusalananda
    Jul 28 at 6:59







  • 1




    mailx, sed (the gnu variety can execute an arbitray shell command, even), ed, ex, vi, xargs, csplit (some few versions), luit (commonly packaged with xterm, so very often available), dc (if your system can process a system() you can get a shell with !)... a lot of these depend on a tty by basic standard... even pax or tar can pull off what you ask with the right amount of crazy... anyway, can you not specify an output file with curl?
    – mikeserv
    Jul 28 at 7:08







  • 1




    @mikeserv You're back! I have been assuming this all means "non-interactive, spawned from execve or similar", but it could be more explicit.
    – Michael Homer
    Jul 28 at 7:36
















up vote
4
down vote

favorite












Suppose I have an environment where there isn't a shell running, so I can't use redirection, pipes, here-documents or other shell-isms, but I can launch a command (through execvp or some similar way). I want to write an arbitrary string to a named file. Is there a standard command that will do something like:



somecommand outputfile 'string'


for instance:



somecommand /proc/sys/net/ipv4/ip_forward '1'


A really dumb example might be:



curl -o /proc/sys/net/ipv4/ip_forward http://example.com/1.txt


where I set up 1.txt to contain the string I want.



Is there a common command that can be abused to do this?







share|improve this question













closed as unclear what you're asking by Kusalananda, schily, Thomas, Jesse_b, Jeff Schaller Jul 28 at 18:38


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    This would never fly, total vector for hacker to exploit.
    – slm♦
    Jul 27 at 23:13










  • Why is writing to a file a vector for hackers? The file is written with the standard privileges of the calling user (so the /proc/sys/... examples would need root or sudo)
    – user1908704
    Jul 28 at 0:06







  • 2




    If you don't have a shell, what's going to run your command? I notice you've tagged the question with "scripting". Are you referring to some other scripting language than a shell scripting language (Perl? Python?). Please clarify the question.
    – Kusalananda
    Jul 28 at 6:59







  • 1




    mailx, sed (the gnu variety can execute an arbitray shell command, even), ed, ex, vi, xargs, csplit (some few versions), luit (commonly packaged with xterm, so very often available), dc (if your system can process a system() you can get a shell with !)... a lot of these depend on a tty by basic standard... even pax or tar can pull off what you ask with the right amount of crazy... anyway, can you not specify an output file with curl?
    – mikeserv
    Jul 28 at 7:08







  • 1




    @mikeserv You're back! I have been assuming this all means "non-interactive, spawned from execve or similar", but it could be more explicit.
    – Michael Homer
    Jul 28 at 7:36












up vote
4
down vote

favorite









up vote
4
down vote

favorite











Suppose I have an environment where there isn't a shell running, so I can't use redirection, pipes, here-documents or other shell-isms, but I can launch a command (through execvp or some similar way). I want to write an arbitrary string to a named file. Is there a standard command that will do something like:



somecommand outputfile 'string'


for instance:



somecommand /proc/sys/net/ipv4/ip_forward '1'


A really dumb example might be:



curl -o /proc/sys/net/ipv4/ip_forward http://example.com/1.txt


where I set up 1.txt to contain the string I want.



Is there a common command that can be abused to do this?







share|improve this question













Suppose I have an environment where there isn't a shell running, so I can't use redirection, pipes, here-documents or other shell-isms, but I can launch a command (through execvp or some similar way). I want to write an arbitrary string to a named file. Is there a standard command that will do something like:



somecommand outputfile 'string'


for instance:



somecommand /proc/sys/net/ipv4/ip_forward '1'


A really dumb example might be:



curl -o /proc/sys/net/ipv4/ip_forward http://example.com/1.txt


where I set up 1.txt to contain the string I want.



Is there a common command that can be abused to do this?









share|improve this question












share|improve this question




share|improve this question








edited Jul 28 at 21:48









Michael Homer

42.1k6107146




42.1k6107146









asked Jul 27 at 23:01









user1908704

1273




1273




closed as unclear what you're asking by Kusalananda, schily, Thomas, Jesse_b, Jeff Schaller Jul 28 at 18:38


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Kusalananda, schily, Thomas, Jesse_b, Jeff Schaller Jul 28 at 18:38


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    This would never fly, total vector for hacker to exploit.
    – slm♦
    Jul 27 at 23:13










  • Why is writing to a file a vector for hackers? The file is written with the standard privileges of the calling user (so the /proc/sys/... examples would need root or sudo)
    – user1908704
    Jul 28 at 0:06







  • 2




    If you don't have a shell, what's going to run your command? I notice you've tagged the question with "scripting". Are you referring to some other scripting language than a shell scripting language (Perl? Python?). Please clarify the question.
    – Kusalananda
    Jul 28 at 6:59







  • 1




    mailx, sed (the gnu variety can execute an arbitray shell command, even), ed, ex, vi, xargs, csplit (some few versions), luit (commonly packaged with xterm, so very often available), dc (if your system can process a system() you can get a shell with !)... a lot of these depend on a tty by basic standard... even pax or tar can pull off what you ask with the right amount of crazy... anyway, can you not specify an output file with curl?
    – mikeserv
    Jul 28 at 7:08







  • 1




    @mikeserv You're back! I have been assuming this all means "non-interactive, spawned from execve or similar", but it could be more explicit.
    – Michael Homer
    Jul 28 at 7:36












  • 1




    This would never fly, total vector for hacker to exploit.
    – slm♦
    Jul 27 at 23:13










  • Why is writing to a file a vector for hackers? The file is written with the standard privileges of the calling user (so the /proc/sys/... examples would need root or sudo)
    – user1908704
    Jul 28 at 0:06







  • 2




    If you don't have a shell, what's going to run your command? I notice you've tagged the question with "scripting". Are you referring to some other scripting language than a shell scripting language (Perl? Python?). Please clarify the question.
    – Kusalananda
    Jul 28 at 6:59







  • 1




    mailx, sed (the gnu variety can execute an arbitray shell command, even), ed, ex, vi, xargs, csplit (some few versions), luit (commonly packaged with xterm, so very often available), dc (if your system can process a system() you can get a shell with !)... a lot of these depend on a tty by basic standard... even pax or tar can pull off what you ask with the right amount of crazy... anyway, can you not specify an output file with curl?
    – mikeserv
    Jul 28 at 7:08







  • 1




    @mikeserv You're back! I have been assuming this all means "non-interactive, spawned from execve or similar", but it could be more explicit.
    – Michael Homer
    Jul 28 at 7:36







1




1




This would never fly, total vector for hacker to exploit.
– slm♦
Jul 27 at 23:13




This would never fly, total vector for hacker to exploit.
– slm♦
Jul 27 at 23:13












Why is writing to a file a vector for hackers? The file is written with the standard privileges of the calling user (so the /proc/sys/... examples would need root or sudo)
– user1908704
Jul 28 at 0:06





Why is writing to a file a vector for hackers? The file is written with the standard privileges of the calling user (so the /proc/sys/... examples would need root or sudo)
– user1908704
Jul 28 at 0:06





2




2




If you don't have a shell, what's going to run your command? I notice you've tagged the question with "scripting". Are you referring to some other scripting language than a shell scripting language (Perl? Python?). Please clarify the question.
– Kusalananda
Jul 28 at 6:59





If you don't have a shell, what's going to run your command? I notice you've tagged the question with "scripting". Are you referring to some other scripting language than a shell scripting language (Perl? Python?). Please clarify the question.
– Kusalananda
Jul 28 at 6:59





1




1




mailx, sed (the gnu variety can execute an arbitray shell command, even), ed, ex, vi, xargs, csplit (some few versions), luit (commonly packaged with xterm, so very often available), dc (if your system can process a system() you can get a shell with !)... a lot of these depend on a tty by basic standard... even pax or tar can pull off what you ask with the right amount of crazy... anyway, can you not specify an output file with curl?
– mikeserv
Jul 28 at 7:08





mailx, sed (the gnu variety can execute an arbitray shell command, even), ed, ex, vi, xargs, csplit (some few versions), luit (commonly packaged with xterm, so very often available), dc (if your system can process a system() you can get a shell with !)... a lot of these depend on a tty by basic standard... even pax or tar can pull off what you ask with the right amount of crazy... anyway, can you not specify an output file with curl?
– mikeserv
Jul 28 at 7:08





1




1




@mikeserv You're back! I have been assuming this all means "non-interactive, spawned from execve or similar", but it could be more explicit.
– Michael Homer
Jul 28 at 7:36




@mikeserv You're back! I have been assuming this all means "non-interactive, spawned from execve or similar", but it could be more explicit.
– Michael Homer
Jul 28 at 7:36










2 Answers
2






active

oldest

votes

















up vote
7
down vote













If you know of any other non-empty file on the system, then with POSIX sed:



sed -e 's/.*/hello world/' -e 'wtarget' -e q otherfile


With GNU sed and just your own non-empty file, you can use:



sed -i.bak -e '$ihello world' -e 'd' foo


With BSD sed, this would work instead:



sed -i.bak -e '$i
hello world' -e d foo


If you're not using a shell then presumably the linebreak isn't an issue.




With ex, if the target file exists:



ex -c '0,$d' -c 's/^/hello world/' -c 'x' foo


This just deletes everything in the file, replaces the first line with "hello world", then writes and quits. You could do the same thing with vi in place of ex. Implementations are not required to support multiple -c options, but they generally do. For many ex implementations the requirement that the file already exist is not enforced.




Also with awk:



awk -v FN=foo -v STR="hello world" 'BEGINprintf(STR) > FN '


will write "hello world" to file "foo".




If there are existing files containing the bytes you want at known locations, you can assemble a file byte by byte over multiple commands with dd (in this case, alphabet contains the alphabet, but it could be a mix of input files):



dd if=alphabet bs=1 skip=7 count=1 of=test
dd if=alphabet bs=1 skip=4 count=1 seek=1 of=test
dd if=alphabet bs=1 skip=11 count=1 seek=2 of=test
dd if=alphabet bs=1 skip=11 count=1 seek=3 of=test
dd if=alphabet bs=1 skip=14 count=1 seek=4 of=test
cat test
hello


From there, just regular cp will work, or you might have been able to put it in-place to start with.




Less commonly, the mispipe command from moreutils allows constructing a shell-free pipe:



mispipe "echo 1" "tee wtarget"


is equivalent to echo 1 | tee wtarget, but returning the exit code of echo. This uses the system() function internally, which doesn't strictly require a shell to exist.




Finally, perl is a common command and will let you write arbitrary programs to do whatever you want on the command line, as will python or any other common scripting language. Similarly, if a shell just isn't "running", but it does exist, sh -c 'echo 1 > target' will work just fine.






share|improve this answer






























    up vote
    5
    down vote













    You could use awk:



    awk 'BEGIN print "Hello" > "/tmp/file"'


    When an awk program consists of only a BEGIN statement, it just interprets that statement and doesn't process any input.



    You can even parameterise this



    awk -v text="World" -v file="/tmp/main" 'BEGIN print text > file'


    Though with that syntax, you'd need to escape backslash characters. Using ARGV doesn't have the problem:



    awk 'BEGIN print ARGV[1] > ARGV[2]' World /tmp/main





    share|improve this answer























    • They both work, but the second requires GNU awk - it doesn't work with BSD awk
      – user1908704
      Jul 28 at 0:11










    • @user1908704, that syntax is standard and comes from nawk. However, in the early versions, you needed -v and the assignment to be separate arguments (-v var=value, not -vvar=value), I've added the extra space.
      – Stéphane Chazelas
      Jul 28 at 6:51










    • @Kusalananda, can you please clarify? Though those are shell command lines, you don't need a shell to execute that awk command, you just need to call awk, with ["awk", "BEGIN print ARGV[1] > ARGV[2]", "World", "/tmp/main"] as arguments (using whatever syntax the non-shell context supports)
      – Stéphane Chazelas
      Jul 28 at 7:04

















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote













    If you know of any other non-empty file on the system, then with POSIX sed:



    sed -e 's/.*/hello world/' -e 'wtarget' -e q otherfile


    With GNU sed and just your own non-empty file, you can use:



    sed -i.bak -e '$ihello world' -e 'd' foo


    With BSD sed, this would work instead:



    sed -i.bak -e '$i
    hello world' -e d foo


    If you're not using a shell then presumably the linebreak isn't an issue.




    With ex, if the target file exists:



    ex -c '0,$d' -c 's/^/hello world/' -c 'x' foo


    This just deletes everything in the file, replaces the first line with "hello world", then writes and quits. You could do the same thing with vi in place of ex. Implementations are not required to support multiple -c options, but they generally do. For many ex implementations the requirement that the file already exist is not enforced.




    Also with awk:



    awk -v FN=foo -v STR="hello world" 'BEGINprintf(STR) > FN '


    will write "hello world" to file "foo".




    If there are existing files containing the bytes you want at known locations, you can assemble a file byte by byte over multiple commands with dd (in this case, alphabet contains the alphabet, but it could be a mix of input files):



    dd if=alphabet bs=1 skip=7 count=1 of=test
    dd if=alphabet bs=1 skip=4 count=1 seek=1 of=test
    dd if=alphabet bs=1 skip=11 count=1 seek=2 of=test
    dd if=alphabet bs=1 skip=11 count=1 seek=3 of=test
    dd if=alphabet bs=1 skip=14 count=1 seek=4 of=test
    cat test
    hello


    From there, just regular cp will work, or you might have been able to put it in-place to start with.




    Less commonly, the mispipe command from moreutils allows constructing a shell-free pipe:



    mispipe "echo 1" "tee wtarget"


    is equivalent to echo 1 | tee wtarget, but returning the exit code of echo. This uses the system() function internally, which doesn't strictly require a shell to exist.




    Finally, perl is a common command and will let you write arbitrary programs to do whatever you want on the command line, as will python or any other common scripting language. Similarly, if a shell just isn't "running", but it does exist, sh -c 'echo 1 > target' will work just fine.






    share|improve this answer



























      up vote
      7
      down vote













      If you know of any other non-empty file on the system, then with POSIX sed:



      sed -e 's/.*/hello world/' -e 'wtarget' -e q otherfile


      With GNU sed and just your own non-empty file, you can use:



      sed -i.bak -e '$ihello world' -e 'd' foo


      With BSD sed, this would work instead:



      sed -i.bak -e '$i
      hello world' -e d foo


      If you're not using a shell then presumably the linebreak isn't an issue.




      With ex, if the target file exists:



      ex -c '0,$d' -c 's/^/hello world/' -c 'x' foo


      This just deletes everything in the file, replaces the first line with "hello world", then writes and quits. You could do the same thing with vi in place of ex. Implementations are not required to support multiple -c options, but they generally do. For many ex implementations the requirement that the file already exist is not enforced.




      Also with awk:



      awk -v FN=foo -v STR="hello world" 'BEGINprintf(STR) > FN '


      will write "hello world" to file "foo".




      If there are existing files containing the bytes you want at known locations, you can assemble a file byte by byte over multiple commands with dd (in this case, alphabet contains the alphabet, but it could be a mix of input files):



      dd if=alphabet bs=1 skip=7 count=1 of=test
      dd if=alphabet bs=1 skip=4 count=1 seek=1 of=test
      dd if=alphabet bs=1 skip=11 count=1 seek=2 of=test
      dd if=alphabet bs=1 skip=11 count=1 seek=3 of=test
      dd if=alphabet bs=1 skip=14 count=1 seek=4 of=test
      cat test
      hello


      From there, just regular cp will work, or you might have been able to put it in-place to start with.




      Less commonly, the mispipe command from moreutils allows constructing a shell-free pipe:



      mispipe "echo 1" "tee wtarget"


      is equivalent to echo 1 | tee wtarget, but returning the exit code of echo. This uses the system() function internally, which doesn't strictly require a shell to exist.




      Finally, perl is a common command and will let you write arbitrary programs to do whatever you want on the command line, as will python or any other common scripting language. Similarly, if a shell just isn't "running", but it does exist, sh -c 'echo 1 > target' will work just fine.






      share|improve this answer

























        up vote
        7
        down vote










        up vote
        7
        down vote









        If you know of any other non-empty file on the system, then with POSIX sed:



        sed -e 's/.*/hello world/' -e 'wtarget' -e q otherfile


        With GNU sed and just your own non-empty file, you can use:



        sed -i.bak -e '$ihello world' -e 'd' foo


        With BSD sed, this would work instead:



        sed -i.bak -e '$i
        hello world' -e d foo


        If you're not using a shell then presumably the linebreak isn't an issue.




        With ex, if the target file exists:



        ex -c '0,$d' -c 's/^/hello world/' -c 'x' foo


        This just deletes everything in the file, replaces the first line with "hello world", then writes and quits. You could do the same thing with vi in place of ex. Implementations are not required to support multiple -c options, but they generally do. For many ex implementations the requirement that the file already exist is not enforced.




        Also with awk:



        awk -v FN=foo -v STR="hello world" 'BEGINprintf(STR) > FN '


        will write "hello world" to file "foo".




        If there are existing files containing the bytes you want at known locations, you can assemble a file byte by byte over multiple commands with dd (in this case, alphabet contains the alphabet, but it could be a mix of input files):



        dd if=alphabet bs=1 skip=7 count=1 of=test
        dd if=alphabet bs=1 skip=4 count=1 seek=1 of=test
        dd if=alphabet bs=1 skip=11 count=1 seek=2 of=test
        dd if=alphabet bs=1 skip=11 count=1 seek=3 of=test
        dd if=alphabet bs=1 skip=14 count=1 seek=4 of=test
        cat test
        hello


        From there, just regular cp will work, or you might have been able to put it in-place to start with.




        Less commonly, the mispipe command from moreutils allows constructing a shell-free pipe:



        mispipe "echo 1" "tee wtarget"


        is equivalent to echo 1 | tee wtarget, but returning the exit code of echo. This uses the system() function internally, which doesn't strictly require a shell to exist.




        Finally, perl is a common command and will let you write arbitrary programs to do whatever you want on the command line, as will python or any other common scripting language. Similarly, if a shell just isn't "running", but it does exist, sh -c 'echo 1 > target' will work just fine.






        share|improve this answer















        If you know of any other non-empty file on the system, then with POSIX sed:



        sed -e 's/.*/hello world/' -e 'wtarget' -e q otherfile


        With GNU sed and just your own non-empty file, you can use:



        sed -i.bak -e '$ihello world' -e 'd' foo


        With BSD sed, this would work instead:



        sed -i.bak -e '$i
        hello world' -e d foo


        If you're not using a shell then presumably the linebreak isn't an issue.




        With ex, if the target file exists:



        ex -c '0,$d' -c 's/^/hello world/' -c 'x' foo


        This just deletes everything in the file, replaces the first line with "hello world", then writes and quits. You could do the same thing with vi in place of ex. Implementations are not required to support multiple -c options, but they generally do. For many ex implementations the requirement that the file already exist is not enforced.




        Also with awk:



        awk -v FN=foo -v STR="hello world" 'BEGINprintf(STR) > FN '


        will write "hello world" to file "foo".




        If there are existing files containing the bytes you want at known locations, you can assemble a file byte by byte over multiple commands with dd (in this case, alphabet contains the alphabet, but it could be a mix of input files):



        dd if=alphabet bs=1 skip=7 count=1 of=test
        dd if=alphabet bs=1 skip=4 count=1 seek=1 of=test
        dd if=alphabet bs=1 skip=11 count=1 seek=2 of=test
        dd if=alphabet bs=1 skip=11 count=1 seek=3 of=test
        dd if=alphabet bs=1 skip=14 count=1 seek=4 of=test
        cat test
        hello


        From there, just regular cp will work, or you might have been able to put it in-place to start with.




        Less commonly, the mispipe command from moreutils allows constructing a shell-free pipe:



        mispipe "echo 1" "tee wtarget"


        is equivalent to echo 1 | tee wtarget, but returning the exit code of echo. This uses the system() function internally, which doesn't strictly require a shell to exist.




        Finally, perl is a common command and will let you write arbitrary programs to do whatever you want on the command line, as will python or any other common scripting language. Similarly, if a shell just isn't "running", but it does exist, sh -c 'echo 1 > target' will work just fine.







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jul 28 at 0:43


























        answered Jul 28 at 0:10









        Michael Homer

        42.1k6107146




        42.1k6107146






















            up vote
            5
            down vote













            You could use awk:



            awk 'BEGIN print "Hello" > "/tmp/file"'


            When an awk program consists of only a BEGIN statement, it just interprets that statement and doesn't process any input.



            You can even parameterise this



            awk -v text="World" -v file="/tmp/main" 'BEGIN print text > file'


            Though with that syntax, you'd need to escape backslash characters. Using ARGV doesn't have the problem:



            awk 'BEGIN print ARGV[1] > ARGV[2]' World /tmp/main





            share|improve this answer























            • They both work, but the second requires GNU awk - it doesn't work with BSD awk
              – user1908704
              Jul 28 at 0:11










            • @user1908704, that syntax is standard and comes from nawk. However, in the early versions, you needed -v and the assignment to be separate arguments (-v var=value, not -vvar=value), I've added the extra space.
              – Stéphane Chazelas
              Jul 28 at 6:51










            • @Kusalananda, can you please clarify? Though those are shell command lines, you don't need a shell to execute that awk command, you just need to call awk, with ["awk", "BEGIN print ARGV[1] > ARGV[2]", "World", "/tmp/main"] as arguments (using whatever syntax the non-shell context supports)
              – Stéphane Chazelas
              Jul 28 at 7:04














            up vote
            5
            down vote













            You could use awk:



            awk 'BEGIN print "Hello" > "/tmp/file"'


            When an awk program consists of only a BEGIN statement, it just interprets that statement and doesn't process any input.



            You can even parameterise this



            awk -v text="World" -v file="/tmp/main" 'BEGIN print text > file'


            Though with that syntax, you'd need to escape backslash characters. Using ARGV doesn't have the problem:



            awk 'BEGIN print ARGV[1] > ARGV[2]' World /tmp/main





            share|improve this answer























            • They both work, but the second requires GNU awk - it doesn't work with BSD awk
              – user1908704
              Jul 28 at 0:11










            • @user1908704, that syntax is standard and comes from nawk. However, in the early versions, you needed -v and the assignment to be separate arguments (-v var=value, not -vvar=value), I've added the extra space.
              – Stéphane Chazelas
              Jul 28 at 6:51










            • @Kusalananda, can you please clarify? Though those are shell command lines, you don't need a shell to execute that awk command, you just need to call awk, with ["awk", "BEGIN print ARGV[1] > ARGV[2]", "World", "/tmp/main"] as arguments (using whatever syntax the non-shell context supports)
              – Stéphane Chazelas
              Jul 28 at 7:04












            up vote
            5
            down vote










            up vote
            5
            down vote









            You could use awk:



            awk 'BEGIN print "Hello" > "/tmp/file"'


            When an awk program consists of only a BEGIN statement, it just interprets that statement and doesn't process any input.



            You can even parameterise this



            awk -v text="World" -v file="/tmp/main" 'BEGIN print text > file'


            Though with that syntax, you'd need to escape backslash characters. Using ARGV doesn't have the problem:



            awk 'BEGIN print ARGV[1] > ARGV[2]' World /tmp/main





            share|improve this answer















            You could use awk:



            awk 'BEGIN print "Hello" > "/tmp/file"'


            When an awk program consists of only a BEGIN statement, it just interprets that statement and doesn't process any input.



            You can even parameterise this



            awk -v text="World" -v file="/tmp/main" 'BEGIN print text > file'


            Though with that syntax, you'd need to escape backslash characters. Using ARGV doesn't have the problem:



            awk 'BEGIN print ARGV[1] > ARGV[2]' World /tmp/main






            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Jul 28 at 6:48









            Stéphane Chazelas

            278k52511841




            278k52511841











            answered Jul 27 at 23:28









            roaima

            39.2k544105




            39.2k544105











            • They both work, but the second requires GNU awk - it doesn't work with BSD awk
              – user1908704
              Jul 28 at 0:11










            • @user1908704, that syntax is standard and comes from nawk. However, in the early versions, you needed -v and the assignment to be separate arguments (-v var=value, not -vvar=value), I've added the extra space.
              – Stéphane Chazelas
              Jul 28 at 6:51










            • @Kusalananda, can you please clarify? Though those are shell command lines, you don't need a shell to execute that awk command, you just need to call awk, with ["awk", "BEGIN print ARGV[1] > ARGV[2]", "World", "/tmp/main"] as arguments (using whatever syntax the non-shell context supports)
              – Stéphane Chazelas
              Jul 28 at 7:04
















            • They both work, but the second requires GNU awk - it doesn't work with BSD awk
              – user1908704
              Jul 28 at 0:11










            • @user1908704, that syntax is standard and comes from nawk. However, in the early versions, you needed -v and the assignment to be separate arguments (-v var=value, not -vvar=value), I've added the extra space.
              – Stéphane Chazelas
              Jul 28 at 6:51










            • @Kusalananda, can you please clarify? Though those are shell command lines, you don't need a shell to execute that awk command, you just need to call awk, with ["awk", "BEGIN print ARGV[1] > ARGV[2]", "World", "/tmp/main"] as arguments (using whatever syntax the non-shell context supports)
              – Stéphane Chazelas
              Jul 28 at 7:04















            They both work, but the second requires GNU awk - it doesn't work with BSD awk
            – user1908704
            Jul 28 at 0:11




            They both work, but the second requires GNU awk - it doesn't work with BSD awk
            – user1908704
            Jul 28 at 0:11












            @user1908704, that syntax is standard and comes from nawk. However, in the early versions, you needed -v and the assignment to be separate arguments (-v var=value, not -vvar=value), I've added the extra space.
            – Stéphane Chazelas
            Jul 28 at 6:51




            @user1908704, that syntax is standard and comes from nawk. However, in the early versions, you needed -v and the assignment to be separate arguments (-v var=value, not -vvar=value), I've added the extra space.
            – Stéphane Chazelas
            Jul 28 at 6:51












            @Kusalananda, can you please clarify? Though those are shell command lines, you don't need a shell to execute that awk command, you just need to call awk, with ["awk", "BEGIN print ARGV[1] > ARGV[2]", "World", "/tmp/main"] as arguments (using whatever syntax the non-shell context supports)
            – Stéphane Chazelas
            Jul 28 at 7:04




            @Kusalananda, can you please clarify? Though those are shell command lines, you don't need a shell to execute that awk command, you just need to call awk, with ["awk", "BEGIN print ARGV[1] > ARGV[2]", "World", "/tmp/main"] as arguments (using whatever syntax the non-shell context supports)
            – Stéphane Chazelas
            Jul 28 at 7:04


            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?