# How to get shell echo in Prolog variables

**URL:** https://swi-prolog.discourse.group/t/how-to-get-shell-echo-in-prolog-variables/6547
**Category:** General
**Tags:** how-to
**Created:** [May 14, 2023, 11:33am UTC](https://swi-prolog.discourse.group/t/how-to-get-shell-echo-in-prolog-variables/6547 "2023-05-14T11:33:57Z")
**Posts on this page:** 4
**Page:** 2

<div class="post-metadata">

### Author: ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)
#### Post date: [May 16, 2023, 8:34am UTC](https://swi-prolog.discourse.group/t/how-to-get-shell-echo-in-prolog-variables/6547/21 "2023-05-16T08:34:34Z")

</div>

> [@brebs](#):
>
> This works:
> 
> ```prolog
> run_command_to_codes([osascript, '-e', 'return POSIX path of (choose folder)'], O, E, Ex),
> atom_codes(OA, O), atom_codes(EA, E).
> 
> ```

I see.  
However still it is not clear for me how to separate a string into such atoms as this case.  
Anyway on this question, I have to take time to learn how the first argument (input command as a list) is processed by process\_create and thread\_create.  
Thanks.

**EDIT**

I tried to follow a sample codes in `help(process_create)` like below, which  
was surprisingly simple, though I am far from familiar with process things.  
Anyway the sample codes seems enough for my purpose to call simple shell command from Prolog codes.

Thanks @brebs and @Jan

```prolog
% ?- choose_folder(X).
%@ 2023-05-17 00:11:05.073 osascript[48546:12223914] +[CATransaction synchronize] called within transaction
%@ X = ['/Users/cantor/Desktop/pdfs/'].

choose_folder(Lines) :-
                setup_call_cleanup(
                    process_create(path(choosefolder), [],
                                   [ stdout(pipe(Out))
                                   ]),
                    read_lines(Out, Lines),
                    close(Out)).
read_lines(Out, Lines) :-
                read_line_to_codes(Out, Line1),
                read_lines(Line1, Out, Lines).

read_lines(end_of_file, _, []) :- !.
read_lines(Codes, Out, [Line|Lines]) :-
                atom_codes(Line, Codes),
                read_line_to_codes(Out, Line2),
                read_lines(Line2, Out, Lines).

```

---

<div class="post-metadata">

### Author: ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)
#### Post date: [May 16, 2023, 3:39pm UTC](https://swi-prolog.discourse.group/t/how-to-get-shell-echo-in-prolog-variables/6547/22 "2023-05-16T15:39:27Z")

</div>

That is all you need for the simple case yes. You can read a bit faster using something like this. read\_string/5 is established common ground with ECLiPSe.

```prolog
read_lines_as_atoms(Stream, Lines) :-
    read_string(Stream, "\n", "", Sep, String),
    ( Sep == -1
    -> Lines = []
    ; atom_string(Line, String),
        Lines = [Line|Rest],
        read_lines_as_atoms(Stream, Rest)
    ).

```

---

<div class="post-metadata">

### Author: ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)
#### Post date: [May 16, 2023, 3:57pm UTC](https://swi-prolog.discourse.group/t/how-to-get-shell-echo-in-prolog-variables/6547/23 "2023-05-16T15:57:47Z")

</div>

> [@jan](#):
>
> Possibly we need some different wording here. … Most of SWI-Prolog’s use of _deprecated_ in documentation merely says _“don’t use this because there are better alternatives”_ …

I suggest “obsolete” instead of “deprecated” for this situation - and add a pointer in the documentation to the functionality that replaces the obsolete code.

---

<div class="post-metadata">

### Author: ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)
#### Post date: [May 16, 2023, 4:11pm UTC](https://swi-prolog.discourse.group/t/how-to-get-shell-echo-in-prolog-variables/6547/24 "2023-05-16T16:11:41Z")

</div>

> [@peter.ludemann](#):
>
> I suggest “obsolete” instead of “deprecated” for this situation - and add a pointer in the documentation to the functionality that replaces the obsolete code.

Thanks. That makes sense. In general deprecated also points/should point at the replacement.

Ideally we’d add `@obsolete` to PlDoc and the rest of the documentation infra and replace many deprecated.

[Previous page](https://swi-prolog.discourse.group/t/how-to-get-shell-echo-in-prolog-variables/6547.md?page=1)
