Haskell stack command line flag for executing an expression












1















If I have a simple Haskell one-liner, what's the flag in ghc or ghci that could execute this from the command line?



I'm looking for something like:



stack ghci -e 'putStrLn "hello world"'


Similar to



$ R --quiet -e "cat('hello world')"
> cat('hello world')
hello world>


or



$ python -c "print('hello world')"
hello world


Edit for 'ghci -e' debug



(This question is resolved with an excellent answer already, but just debugging that the flag seems like it /should/ work above...)



Weirdly couldn't get the seemingly supported ghci -e working for me. Testing it is not just my machine, I also ran this on Ubuntu and had the same issue:



FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update
&& apt-get install --yes curl
&& curl -sSL https://get.haskellstack.org/ | sh
&& export HOME=/root/.local/bin:$HOME
&& stack ghci -e 'putStrLn "hello world"'


Then



$ docker build .


Produced...



Stack has been installed to: /usr/local/bin/stack

WARNING: '/root/.local/bin' is not on your PATH.
For best results, please add it to the beginning of PATH in your profile.

Invalid option `-e'

Usage: stack ghci [TARGET/FILE] [--ghci-options OPTIONS] [--ghc-options OPTIONS]
[--flag PACKAGE:[-]FLAG] [--with-ghc GHC] [--[no-]load]
[--package ARG] [--main-is TARGET] [--load-local-deps]
[--[no-]package-hiding] [--only-main] [--trace] [--profile]
[--no-strip] [--[no-]test] [--[no-]bench] [--help]
Run ghci in the context of package(s) (experimental)
The command '/bin/sh -c apt-get update && apt-get install --yes curl && curl -sSL https://get.haskellstack.org/ | sh && export HOME=/root/.local/bin:$HOME && stack ghci -e 'putStrLn "hello world"'' returned a non-zero code: 1









share|improve this question




















  • 3





    stack eval 'putStrLn "hello world"'

    – Redu
    Nov 25 '18 at 19:02











  • @Redu, this works, can you post as an answer I can accept?

    – Mittenchops
    Nov 25 '18 at 19:06
















1















If I have a simple Haskell one-liner, what's the flag in ghc or ghci that could execute this from the command line?



I'm looking for something like:



stack ghci -e 'putStrLn "hello world"'


Similar to



$ R --quiet -e "cat('hello world')"
> cat('hello world')
hello world>


or



$ python -c "print('hello world')"
hello world


Edit for 'ghci -e' debug



(This question is resolved with an excellent answer already, but just debugging that the flag seems like it /should/ work above...)



Weirdly couldn't get the seemingly supported ghci -e working for me. Testing it is not just my machine, I also ran this on Ubuntu and had the same issue:



FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update
&& apt-get install --yes curl
&& curl -sSL https://get.haskellstack.org/ | sh
&& export HOME=/root/.local/bin:$HOME
&& stack ghci -e 'putStrLn "hello world"'


Then



$ docker build .


Produced...



Stack has been installed to: /usr/local/bin/stack

WARNING: '/root/.local/bin' is not on your PATH.
For best results, please add it to the beginning of PATH in your profile.

Invalid option `-e'

Usage: stack ghci [TARGET/FILE] [--ghci-options OPTIONS] [--ghc-options OPTIONS]
[--flag PACKAGE:[-]FLAG] [--with-ghc GHC] [--[no-]load]
[--package ARG] [--main-is TARGET] [--load-local-deps]
[--[no-]package-hiding] [--only-main] [--trace] [--profile]
[--no-strip] [--[no-]test] [--[no-]bench] [--help]
Run ghci in the context of package(s) (experimental)
The command '/bin/sh -c apt-get update && apt-get install --yes curl && curl -sSL https://get.haskellstack.org/ | sh && export HOME=/root/.local/bin:$HOME && stack ghci -e 'putStrLn "hello world"'' returned a non-zero code: 1









share|improve this question




















  • 3





    stack eval 'putStrLn "hello world"'

    – Redu
    Nov 25 '18 at 19:02











  • @Redu, this works, can you post as an answer I can accept?

    – Mittenchops
    Nov 25 '18 at 19:06














1












1








1








If I have a simple Haskell one-liner, what's the flag in ghc or ghci that could execute this from the command line?



I'm looking for something like:



stack ghci -e 'putStrLn "hello world"'


Similar to



$ R --quiet -e "cat('hello world')"
> cat('hello world')
hello world>


or



$ python -c "print('hello world')"
hello world


Edit for 'ghci -e' debug



(This question is resolved with an excellent answer already, but just debugging that the flag seems like it /should/ work above...)



Weirdly couldn't get the seemingly supported ghci -e working for me. Testing it is not just my machine, I also ran this on Ubuntu and had the same issue:



FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update
&& apt-get install --yes curl
&& curl -sSL https://get.haskellstack.org/ | sh
&& export HOME=/root/.local/bin:$HOME
&& stack ghci -e 'putStrLn "hello world"'


Then



$ docker build .


Produced...



Stack has been installed to: /usr/local/bin/stack

WARNING: '/root/.local/bin' is not on your PATH.
For best results, please add it to the beginning of PATH in your profile.

Invalid option `-e'

Usage: stack ghci [TARGET/FILE] [--ghci-options OPTIONS] [--ghc-options OPTIONS]
[--flag PACKAGE:[-]FLAG] [--with-ghc GHC] [--[no-]load]
[--package ARG] [--main-is TARGET] [--load-local-deps]
[--[no-]package-hiding] [--only-main] [--trace] [--profile]
[--no-strip] [--[no-]test] [--[no-]bench] [--help]
Run ghci in the context of package(s) (experimental)
The command '/bin/sh -c apt-get update && apt-get install --yes curl && curl -sSL https://get.haskellstack.org/ | sh && export HOME=/root/.local/bin:$HOME && stack ghci -e 'putStrLn "hello world"'' returned a non-zero code: 1









share|improve this question
















If I have a simple Haskell one-liner, what's the flag in ghc or ghci that could execute this from the command line?



I'm looking for something like:



stack ghci -e 'putStrLn "hello world"'


Similar to



$ R --quiet -e "cat('hello world')"
> cat('hello world')
hello world>


or



$ python -c "print('hello world')"
hello world


Edit for 'ghci -e' debug



(This question is resolved with an excellent answer already, but just debugging that the flag seems like it /should/ work above...)



Weirdly couldn't get the seemingly supported ghci -e working for me. Testing it is not just my machine, I also ran this on Ubuntu and had the same issue:



FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update
&& apt-get install --yes curl
&& curl -sSL https://get.haskellstack.org/ | sh
&& export HOME=/root/.local/bin:$HOME
&& stack ghci -e 'putStrLn "hello world"'


Then



$ docker build .


Produced...



Stack has been installed to: /usr/local/bin/stack

WARNING: '/root/.local/bin' is not on your PATH.
For best results, please add it to the beginning of PATH in your profile.

Invalid option `-e'

Usage: stack ghci [TARGET/FILE] [--ghci-options OPTIONS] [--ghc-options OPTIONS]
[--flag PACKAGE:[-]FLAG] [--with-ghc GHC] [--[no-]load]
[--package ARG] [--main-is TARGET] [--load-local-deps]
[--[no-]package-hiding] [--only-main] [--trace] [--profile]
[--no-strip] [--[no-]test] [--[no-]bench] [--help]
Run ghci in the context of package(s) (experimental)
The command '/bin/sh -c apt-get update && apt-get install --yes curl && curl -sSL https://get.haskellstack.org/ | sh && export HOME=/root/.local/bin:$HOME && stack ghci -e 'putStrLn "hello world"'' returned a non-zero code: 1






haskell haskell-stack






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 20:49







Mittenchops

















asked Nov 25 '18 at 18:57









MittenchopsMittenchops

6,5312268140




6,5312268140








  • 3





    stack eval 'putStrLn "hello world"'

    – Redu
    Nov 25 '18 at 19:02











  • @Redu, this works, can you post as an answer I can accept?

    – Mittenchops
    Nov 25 '18 at 19:06














  • 3





    stack eval 'putStrLn "hello world"'

    – Redu
    Nov 25 '18 at 19:02











  • @Redu, this works, can you post as an answer I can accept?

    – Mittenchops
    Nov 25 '18 at 19:06








3




3





stack eval 'putStrLn "hello world"'

– Redu
Nov 25 '18 at 19:02





stack eval 'putStrLn "hello world"'

– Redu
Nov 25 '18 at 19:02













@Redu, this works, can you post as an answer I can accept?

– Mittenchops
Nov 25 '18 at 19:06





@Redu, this works, can you post as an answer I can accept?

– Mittenchops
Nov 25 '18 at 19:06












2 Answers
2






active

oldest

votes


















6














If you check $ stack --help at one point you see that



eval                     Evaluate some haskell code inline. Shortcut for
'stack exec ghc -- -e CODE'


So instead of doing like



$ stack exec ghc -- -e 'putStrLn "hello world"'
hello world


you may do like



$ stack eval 'putStrLn "hello world"'
hello world





share|improve this answer































    2














    In fact you already have this flag: it is just:




    -e expr


    Evaluate expr; see eval-mode for details




    So you can write it like:



    ghci -e 'putStrLn "hello world"'


    In fact if you use stack ghci, you just open ghci with your application, but the -e flag is not "stack-specific".



    For example:



    $ ghci -e 'putStrLn "hello world"'
    hello world





    share|improve this answer


























    • Hmm, I get: $ stack ghci -e 'putStrLn "hello world"' Invalid option -e' ` And don't otherwise have ghc installed outside of stack, if possible to keep it that way: $ ghc -e 'putStrLn "hello world"' Command 'ghc' not found, but can be installed with: sudo apt install ghc

      – Mittenchops
      Nov 25 '18 at 19:05











    • @Mittenchops: what version of ghci do you use? This is already supported since 7.6 (or even eariler) iirc.

      – Willem Van Onsem
      Nov 25 '18 at 19:07











    • $ stack ghci Configuring GHCi with the following packages: GHCi, version 8.4.3: http://www.haskell.org/ghc/ :? for help

      – Mittenchops
      Nov 25 '18 at 19:08













    • @Mittenchops: in 8.4.3, this flag is also supported: downloads.haskell.org/~ghc/8.4.3/docs/html/users_guide/…, strange

      – Willem Van Onsem
      Nov 25 '18 at 19:09








    • 1





      @Mittenchops: well I work with Ubuntu as well, so this makes it even more strange :S

      – Willem Van Onsem
      Nov 25 '18 at 19:26











    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%2f53470824%2fhaskell-stack-command-line-flag-for-executing-an-expression%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    If you check $ stack --help at one point you see that



    eval                     Evaluate some haskell code inline. Shortcut for
    'stack exec ghc -- -e CODE'


    So instead of doing like



    $ stack exec ghc -- -e 'putStrLn "hello world"'
    hello world


    you may do like



    $ stack eval 'putStrLn "hello world"'
    hello world





    share|improve this answer




























      6














      If you check $ stack --help at one point you see that



      eval                     Evaluate some haskell code inline. Shortcut for
      'stack exec ghc -- -e CODE'


      So instead of doing like



      $ stack exec ghc -- -e 'putStrLn "hello world"'
      hello world


      you may do like



      $ stack eval 'putStrLn "hello world"'
      hello world





      share|improve this answer


























        6












        6








        6







        If you check $ stack --help at one point you see that



        eval                     Evaluate some haskell code inline. Shortcut for
        'stack exec ghc -- -e CODE'


        So instead of doing like



        $ stack exec ghc -- -e 'putStrLn "hello world"'
        hello world


        you may do like



        $ stack eval 'putStrLn "hello world"'
        hello world





        share|improve this answer













        If you check $ stack --help at one point you see that



        eval                     Evaluate some haskell code inline. Shortcut for
        'stack exec ghc -- -e CODE'


        So instead of doing like



        $ stack exec ghc -- -e 'putStrLn "hello world"'
        hello world


        you may do like



        $ stack eval 'putStrLn "hello world"'
        hello world






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 19:12









        ReduRedu

        13.1k22537




        13.1k22537

























            2














            In fact you already have this flag: it is just:




            -e expr


            Evaluate expr; see eval-mode for details




            So you can write it like:



            ghci -e 'putStrLn "hello world"'


            In fact if you use stack ghci, you just open ghci with your application, but the -e flag is not "stack-specific".



            For example:



            $ ghci -e 'putStrLn "hello world"'
            hello world





            share|improve this answer


























            • Hmm, I get: $ stack ghci -e 'putStrLn "hello world"' Invalid option -e' ` And don't otherwise have ghc installed outside of stack, if possible to keep it that way: $ ghc -e 'putStrLn "hello world"' Command 'ghc' not found, but can be installed with: sudo apt install ghc

              – Mittenchops
              Nov 25 '18 at 19:05











            • @Mittenchops: what version of ghci do you use? This is already supported since 7.6 (or even eariler) iirc.

              – Willem Van Onsem
              Nov 25 '18 at 19:07











            • $ stack ghci Configuring GHCi with the following packages: GHCi, version 8.4.3: http://www.haskell.org/ghc/ :? for help

              – Mittenchops
              Nov 25 '18 at 19:08













            • @Mittenchops: in 8.4.3, this flag is also supported: downloads.haskell.org/~ghc/8.4.3/docs/html/users_guide/…, strange

              – Willem Van Onsem
              Nov 25 '18 at 19:09








            • 1





              @Mittenchops: well I work with Ubuntu as well, so this makes it even more strange :S

              – Willem Van Onsem
              Nov 25 '18 at 19:26
















            2














            In fact you already have this flag: it is just:




            -e expr


            Evaluate expr; see eval-mode for details




            So you can write it like:



            ghci -e 'putStrLn "hello world"'


            In fact if you use stack ghci, you just open ghci with your application, but the -e flag is not "stack-specific".



            For example:



            $ ghci -e 'putStrLn "hello world"'
            hello world





            share|improve this answer


























            • Hmm, I get: $ stack ghci -e 'putStrLn "hello world"' Invalid option -e' ` And don't otherwise have ghc installed outside of stack, if possible to keep it that way: $ ghc -e 'putStrLn "hello world"' Command 'ghc' not found, but can be installed with: sudo apt install ghc

              – Mittenchops
              Nov 25 '18 at 19:05











            • @Mittenchops: what version of ghci do you use? This is already supported since 7.6 (or even eariler) iirc.

              – Willem Van Onsem
              Nov 25 '18 at 19:07











            • $ stack ghci Configuring GHCi with the following packages: GHCi, version 8.4.3: http://www.haskell.org/ghc/ :? for help

              – Mittenchops
              Nov 25 '18 at 19:08













            • @Mittenchops: in 8.4.3, this flag is also supported: downloads.haskell.org/~ghc/8.4.3/docs/html/users_guide/…, strange

              – Willem Van Onsem
              Nov 25 '18 at 19:09








            • 1





              @Mittenchops: well I work with Ubuntu as well, so this makes it even more strange :S

              – Willem Van Onsem
              Nov 25 '18 at 19:26














            2












            2








            2







            In fact you already have this flag: it is just:




            -e expr


            Evaluate expr; see eval-mode for details




            So you can write it like:



            ghci -e 'putStrLn "hello world"'


            In fact if you use stack ghci, you just open ghci with your application, but the -e flag is not "stack-specific".



            For example:



            $ ghci -e 'putStrLn "hello world"'
            hello world





            share|improve this answer















            In fact you already have this flag: it is just:




            -e expr


            Evaluate expr; see eval-mode for details




            So you can write it like:



            ghci -e 'putStrLn "hello world"'


            In fact if you use stack ghci, you just open ghci with your application, but the -e flag is not "stack-specific".



            For example:



            $ ghci -e 'putStrLn "hello world"'
            hello world






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 25 '18 at 19:08

























            answered Nov 25 '18 at 19:04









            Willem Van OnsemWillem Van Onsem

            150k16146235




            150k16146235













            • Hmm, I get: $ stack ghci -e 'putStrLn "hello world"' Invalid option -e' ` And don't otherwise have ghc installed outside of stack, if possible to keep it that way: $ ghc -e 'putStrLn "hello world"' Command 'ghc' not found, but can be installed with: sudo apt install ghc

              – Mittenchops
              Nov 25 '18 at 19:05











            • @Mittenchops: what version of ghci do you use? This is already supported since 7.6 (or even eariler) iirc.

              – Willem Van Onsem
              Nov 25 '18 at 19:07











            • $ stack ghci Configuring GHCi with the following packages: GHCi, version 8.4.3: http://www.haskell.org/ghc/ :? for help

              – Mittenchops
              Nov 25 '18 at 19:08













            • @Mittenchops: in 8.4.3, this flag is also supported: downloads.haskell.org/~ghc/8.4.3/docs/html/users_guide/…, strange

              – Willem Van Onsem
              Nov 25 '18 at 19:09








            • 1





              @Mittenchops: well I work with Ubuntu as well, so this makes it even more strange :S

              – Willem Van Onsem
              Nov 25 '18 at 19:26



















            • Hmm, I get: $ stack ghci -e 'putStrLn "hello world"' Invalid option -e' ` And don't otherwise have ghc installed outside of stack, if possible to keep it that way: $ ghc -e 'putStrLn "hello world"' Command 'ghc' not found, but can be installed with: sudo apt install ghc

              – Mittenchops
              Nov 25 '18 at 19:05











            • @Mittenchops: what version of ghci do you use? This is already supported since 7.6 (or even eariler) iirc.

              – Willem Van Onsem
              Nov 25 '18 at 19:07











            • $ stack ghci Configuring GHCi with the following packages: GHCi, version 8.4.3: http://www.haskell.org/ghc/ :? for help

              – Mittenchops
              Nov 25 '18 at 19:08













            • @Mittenchops: in 8.4.3, this flag is also supported: downloads.haskell.org/~ghc/8.4.3/docs/html/users_guide/…, strange

              – Willem Van Onsem
              Nov 25 '18 at 19:09








            • 1





              @Mittenchops: well I work with Ubuntu as well, so this makes it even more strange :S

              – Willem Van Onsem
              Nov 25 '18 at 19:26

















            Hmm, I get: $ stack ghci -e 'putStrLn "hello world"' Invalid option -e' ` And don't otherwise have ghc installed outside of stack, if possible to keep it that way: $ ghc -e 'putStrLn "hello world"' Command 'ghc' not found, but can be installed with: sudo apt install ghc

            – Mittenchops
            Nov 25 '18 at 19:05





            Hmm, I get: $ stack ghci -e 'putStrLn "hello world"' Invalid option -e' ` And don't otherwise have ghc installed outside of stack, if possible to keep it that way: $ ghc -e 'putStrLn "hello world"' Command 'ghc' not found, but can be installed with: sudo apt install ghc

            – Mittenchops
            Nov 25 '18 at 19:05













            @Mittenchops: what version of ghci do you use? This is already supported since 7.6 (or even eariler) iirc.

            – Willem Van Onsem
            Nov 25 '18 at 19:07





            @Mittenchops: what version of ghci do you use? This is already supported since 7.6 (or even eariler) iirc.

            – Willem Van Onsem
            Nov 25 '18 at 19:07













            $ stack ghci Configuring GHCi with the following packages: GHCi, version 8.4.3: http://www.haskell.org/ghc/ :? for help

            – Mittenchops
            Nov 25 '18 at 19:08







            $ stack ghci Configuring GHCi with the following packages: GHCi, version 8.4.3: http://www.haskell.org/ghc/ :? for help

            – Mittenchops
            Nov 25 '18 at 19:08















            @Mittenchops: in 8.4.3, this flag is also supported: downloads.haskell.org/~ghc/8.4.3/docs/html/users_guide/…, strange

            – Willem Van Onsem
            Nov 25 '18 at 19:09







            @Mittenchops: in 8.4.3, this flag is also supported: downloads.haskell.org/~ghc/8.4.3/docs/html/users_guide/…, strange

            – Willem Van Onsem
            Nov 25 '18 at 19:09






            1




            1





            @Mittenchops: well I work with Ubuntu as well, so this makes it even more strange :S

            – Willem Van Onsem
            Nov 25 '18 at 19:26





            @Mittenchops: well I work with Ubuntu as well, so this makes it even more strange :S

            – Willem Van Onsem
            Nov 25 '18 at 19:26


















            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%2f53470824%2fhaskell-stack-command-line-flag-for-executing-an-expression%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

            Wiesbaden

            Marschland

            Dieringhausen