Erlang -- escript

Erlang Run-Time System Application (ERTS)

Reference Manual

Version 10.7

- User's Guide
- Reference Manual
- Internal Documentation
- Release Notes
- PDF
- Top

- Expand All
- Contract All

### Table of Contents

- atomics
  - Top of manual page
  - add/3
  - add_get/3
  - compare_exchange/4
  - exchange/3
  - get/2
  - info/1
  - new/2
  - put/3
  - sub/3
  - sub_get/3
- counters
  - Top of manual page
  - add/3
  - get/2
  - info/1
  - new/2
  - put/3
  - sub/3
- driver_entry
- epmd
- erl
- erlang
  - Top of manual page
  - abs/1
  - adler32/1
  - adler32/2
  - adler32_combine/3
  - append_element/2
  - apply/2
  - apply/3
  - atom_to_binary/2
  - atom_to_list/1
  - binary_part/2
  - binary_part/3
  - binary_to_atom/2
  - binary_to_existing_atom/2
  - binary_to_float/1
  - binary_to_integer/1
  - binary_to_integer/2
  - binary_to_list/1
  - binary_to_list/3
  - binary_to_term/1
  - binary_to_term/2
  - bit_size/1
  - bitstring_to_list/1
  - bump_reductions/1
  - byte_size/1
  - cancel_timer/1
  - cancel_timer/2
  - ceil/1
  - check_old_code/1
  - check_process_code/2
  - check_process_code/3
  - convert_time_unit/3
  - crc32/1
  - crc32/2
  - crc32_combine/3
  - date/0
  - decode_packet/3
  - delete_element/2
  - delete_module/1
  - demonitor/1
  - demonitor/2
  - disconnect_node/1
  - display/1
  - dist_ctrl_get_data/1
  - dist_ctrl_get_data_notification/1
  - dist_ctrl_get_opt/2
  - dist_ctrl_input_handler/2
  - dist_ctrl_put_data/2
  - dist_ctrl_set_opt/3
  - element/2
  - erase/0
  - erase/1
  - error/1
  - error/2
  - exit/1
  - exit/2
  - external_size/1
  - external_size/2
  - float/1
  - float_to_binary/1
  - float_to_binary/2
  - float_to_list/1
  - float_to_list/2
  - floor/1
  - fun_info/1
  - fun_info/2
  - fun_to_list/1
  - function_exported/3
  - garbage_collect/0
  - garbage_collect/1
  - garbage_collect/2
  - get/0
  - get/1
  - get_cookie/0
  - get_keys/0
  - get_keys/1
  - get_stacktrace/0
  - group_leader/0
  - group_leader/2
  - halt/0
  - halt/1
  - halt/2
  - hd/1
  - hibernate/3
  - insert_element/3
  - integer_to_binary/1
  - integer_to_binary/2
  - integer_to_list/1
  - integer_to_list/2
  - iolist_size/1
  - iolist_to_binary/1
  - iolist_to_iovec/1
  - is_alive/0
  - is_atom/1
  - is_binary/1
  - is_bitstring/1
  - is_boolean/1
  - is_builtin/3
  - is_float/1
  - is_function/1
  - is_function/2
  - is_integer/1
  - is_list/1
  - is_map/1
  - is_map_key/2
  - is_number/1
  - is_pid/1
  - is_port/1
  - is_process_alive/1
  - is_record/2
  - is_record/3
  - is_reference/1
  - is_tuple/1
  - length/1
  - link/1
  - list_to_atom/1
  - list_to_binary/1
  - list_to_bitstring/1
  - list_to_existing_atom/1
  - list_to_float/1
  - list_to_integer/1
  - list_to_integer/2
  - list_to_pid/1
  - list_to_port/1
  - list_to_ref/1
  - list_to_tuple/1
  - load_module/2
  - load_nif/2
  - loaded/0
  - localtime/0
  - localtime_to_universaltime/1
  - localtime_to_universaltime/2
  - make_ref/0
  - make_tuple/2
  - make_tuple/3
  - map_get/2
  - map_size/1
  - match_spec_test/3
  - max/2
  - md5/1
  - md5_final/1
  - md5_init/0
  - md5_update/2
  - memory/0
  - memory/1
  - min/2
  - module_loaded/1
  - monitor/2
  - monitor_node/2
  - monitor_node/3
  - monotonic_time/0
  - monotonic_time/1
  - nif_error/1
  - nif_error/2
  - node/0
  - node/1
  - nodes/0
  - nodes/1
  - now/0
  - open_port/2
  - phash/2
  - phash2/1
  - phash2/2
  - pid_to_list/1
  - port_call/3
  - port_close/1
  - port_command/2
  - port_command/3
  - port_connect/2
  - port_control/3
  - port_info/1
  - port_info/2
  - port_to_list/1
  - ports/0
  - pre_loaded/0
  - process_display/2
  - process_flag/2
  - process_flag/3
  - process_info/1
  - process_info/2
  - processes/0
  - purge_module/1
  - put/2
  - raise/3
  - read_timer/1
  - read_timer/2
  - ref_to_list/1
  - register/2
  - registered/0
  - resume_process/1
  - round/1
  - self/0
  - send/2
  - send/3
  - send_after/3
  - send_after/4
  - send_nosuspend/2
  - send_nosuspend/3
  - set_cookie/2
  - setelement/3
  - size/1
  - spawn/1
  - spawn/2
  - spawn/3
  - spawn/4
  - spawn_link/1
  - spawn_link/2
  - spawn_link/3
  - spawn_link/4
  - spawn_monitor/1
  - spawn_monitor/3
  - spawn_opt/2
  - spawn_opt/3
  - spawn_opt/4
  - spawn_opt/5
  - split_binary/2
  - start_timer/3
  - start_timer/4
  - statistics/1
  - suspend_process/1
  - suspend_process/2
  - system_flag/2
  - system_info/1
  - system_monitor/0
  - system_monitor/1
  - system_monitor/2
  - system_profile/0
  - system_profile/2
  - system_time/0
  - system_time/1
  - term_to_binary/1
  - term_to_binary/2
  - throw/1
  - time/0
  - time_offset/0
  - time_offset/1
  - timestamp/0
  - tl/1
  - trace/3
  - trace_delivered/1
  - trace_info/2
  - trace_pattern/2
  - trace_pattern/3
  - trunc/1
  - tuple_size/1
  - tuple_to_list/1
  - unique_integer/0
  - unique_integer/1
  - universaltime/0
  - universaltime_to_localtime/1
  - unlink/1
  - unregister/1
  - whereis/1
  - yield/0
- erlc
- erl_driver
  - Top of manual page
  - add_driver_entry()
  - driver_alloc()
  - driver_alloc_binary()
  - driver_async()
  - driver_async_port_key()
  - driver_binary_dec_refc()
  - driver_binary_get_refc()
  - driver_binary_inc_refc()
  - driver_caller()
  - driver_cancel_timer()
  - driver_compare_monitors()
  - driver_connected()
  - driver_create_port()
  - driver_demonitor_process()
  - driver_deq()
  - driver_enq()
  - driver_enq_bin()
  - driver_enqv()
  - driver_failure()
  - driver_failure_atom()
  - driver_failure_eof()
  - driver_failure_posix()
  - driver_free()
  - driver_free_binary()
  - driver_get_monitored_process()
  - driver_get_now()
  - driver_lock_driver()
  - driver_mk_atom()
  - driver_mk_port()
  - driver_monitor_process()
  - driver_output()
  - driver_output2()
  - driver_output_binary()
  - driver_output_term()
  - driver_outputv()
  - driver_pdl_create()
  - driver_pdl_dec_refc()
  - driver_pdl_get_refc()
  - driver_pdl_inc_refc()
  - driver_pdl_lock()
  - driver_pdl_unlock()
  - driver_peekq()
  - driver_peekqv()
  - driver_pushq()
  - driver_pushq_bin()
  - driver_pushqv()
  - driver_read_timer()
  - driver_realloc()
  - driver_realloc_binary()
  - driver_select()
  - driver_send_term()
  - driver_set_timer()
  - driver_sizeq()
  - driver_system_info()
  - driver_vec_to_buf()
  - erl_drv_busy_msgq_limits()
  - erl_drv_cond_broadcast()
  - erl_drv_cond_create()
  - erl_drv_cond_destroy()
  - erl_drv_cond_name()
  - erl_drv_cond_signal()
  - erl_drv_cond_wait()
  - erl_drv_consume_timeslice()
  - erl_drv_convert_time_unit()
  - erl_drv_equal_tids()
  - erl_drv_getenv()
  - erl_drv_init_ack()
  - erl_drv_monotonic_time()
  - erl_drv_mutex_create()
  - erl_drv_mutex_destroy()
  - erl_drv_mutex_lock()
  - erl_drv_mutex_name()
  - erl_drv_mutex_trylock()
  - erl_drv_mutex_unlock()
  - erl_drv_output_term()
  - erl_drv_putenv()
  - erl_drv_rwlock_create()
  - erl_drv_rwlock_destroy()
  - erl_drv_rwlock_name()
  - erl_drv_rwlock_rlock()
  - erl_drv_rwlock_runlock()
  - erl_drv_rwlock_rwlock()
  - erl_drv_rwlock_rwunlock()
  - erl_drv_rwlock_tryrlock()
  - erl_drv_rwlock_tryrwlock()
  - erl_drv_send_term()
  - erl_drv_set_os_pid()
  - erl_drv_thread_create()
  - erl_drv_thread_exit()
  - erl_drv_thread_join()
  - erl_drv_thread_name()
  - erl_drv_thread_opts_create()
  - erl_drv_thread_opts_destroy()
  - erl_drv_thread_self()
  - erl_drv_time_offset()
  - erl_drv_tsd_get()
  - erl_drv_tsd_key_create()
  - erl_drv_tsd_key_destroy()
  - erl_drv_tsd_set()
  - erl_errno_id()
  - remove_driver_entry()
  - set_busy_port()
  - set_port_control_flags()
- erl_nif
  - Top of manual page
  - enif_alloc()
  - enif_alloc_binary()
  - enif_alloc_env()
  - enif_alloc_resource()
  - enif_binary_to_term()
  - enif_clear_env()
  - enif_compare()
  - enif_compare_monitors()
  - enif_compare_pids()
  - enif_cond_broadcast()
  - enif_cond_create()
  - enif_cond_destroy()
  - enif_cond_name()
  - enif_cond_signal()
  - enif_cond_wait()
  - enif_consume_timeslice()
  - enif_convert_time_unit()
  - enif_cpu_time()
  - enif_demonitor_process()
  - enif_equal_tids()
  - enif_fprintf()
  - enif_free()
  - enif_free_env()
  - enif_free_iovec()
  - enif_get_atom()
  - enif_get_atom_length()
  - enif_get_double()
  - enif_get_int()
  - enif_get_int64()
  - enif_get_list_cell()
  - enif_get_list_length()
  - enif_get_local_pid()
  - enif_get_local_port()
  - enif_get_long()
  - enif_get_map_size()
  - enif_get_map_value()
  - enif_get_resource()
  - enif_get_string()
  - enif_get_tuple()
  - enif_get_uint()
  - enif_get_uint64()
  - enif_get_ulong()
  - enif_getenv()
  - enif_has_pending_exception()
  - enif_hash()
  - enif_inspect_binary()
  - enif_inspect_iolist_as_binary()
  - enif_inspect_iovec()
  - enif_ioq_create()
  - enif_ioq_deq()
  - enif_ioq_destroy()
  - enif_ioq_enq_binary()
  - enif_ioq_enqv()
  - enif_ioq_peek()
  - enif_ioq_peek_head()
  - enif_ioq_size()
  - enif_is_atom()
  - enif_is_binary()
  - enif_is_current_process_alive()
  - enif_is_empty_list()
  - enif_is_exception()
  - enif_is_fun()
  - enif_is_identical()
  - enif_is_list()
  - enif_is_map()
  - enif_is_number()
  - enif_is_pid()
  - enif_is_pid_undefined()
  - enif_is_port()
  - enif_is_port_alive()
  - enif_is_process_alive()
  - enif_is_ref()
  - enif_is_tuple()
  - enif_keep_resource()
  - enif_make_atom()
  - enif_make_atom_len()
  - enif_make_badarg()
  - enif_make_binary()
  - enif_make_copy()
  - enif_make_double()
  - enif_make_existing_atom()
  - enif_make_existing_atom_len()
  - enif_make_int()
  - enif_make_int64()
  - enif_make_list()
  - enif_make_list1()
  - enif_make_list2()
  - enif_make_list3()
  - enif_make_list4()
  - enif_make_list5()
  - enif_make_list6()
  - enif_make_list7()
  - enif_make_list8()
  - enif_make_list9()
  - enif_make_list_cell()
  - enif_make_list_from_array()
  - enif_make_long()
  - enif_make_map_from_arrays()
  - enif_make_map_put()
  - enif_make_map_remove()
  - enif_make_map_update()
  - enif_make_monitor_term()
  - enif_make_new_binary()
  - enif_make_new_map()
  - enif_make_pid()
  - enif_make_ref()
  - enif_make_resource()
  - enif_make_resource_binary()
  - enif_make_reverse_list()
  - enif_make_string()
  - enif_make_string_len()
  - enif_make_sub_binary()
  - enif_make_tuple()
  - enif_make_tuple1()
  - enif_make_tuple2()
  - enif_make_tuple3()
  - enif_make_tuple4()
  - enif_make_tuple5()
  - enif_make_tuple6()
  - enif_make_tuple7()
  - enif_make_tuple8()
  - enif_make_tuple9()
  - enif_make_tuple_from_array()
  - enif_make_uint()
  - enif_make_uint64()
  - enif_make_ulong()
  - enif_make_unique_integer()
  - enif_map_iterator_create()
  - enif_map_iterator_destroy()
  - enif_map_iterator_get_pair()
  - enif_map_iterator_is_head()
  - enif_map_iterator_is_tail()
  - enif_map_iterator_next()
  - enif_map_iterator_prev()
  - enif_monitor_process()
  - enif_monotonic_time()
  - enif_mutex_create()
  - enif_mutex_destroy()
  - enif_mutex_lock()
  - enif_mutex_name()
  - enif_mutex_trylock()
  - enif_mutex_unlock()
  - enif_now_time()
  - enif_open_resource_type()
  - enif_open_resource_type_x()
  - enif_port_command()
  - enif_priv_data()
  - enif_raise_exception()
  - enif_realloc()
  - enif_realloc_binary()
  - enif_release_binary()
  - enif_release_resource()
  - enif_rwlock_create()
  - enif_rwlock_destroy()
  - enif_rwlock_name()
  - enif_rwlock_rlock()
  - enif_rwlock_runlock()
  - enif_rwlock_rwlock()
  - enif_rwlock_rwunlock()
  - enif_rwlock_tryrlock()
  - enif_rwlock_tryrwlock()
  - enif_schedule_nif()
  - enif_select()
  - enif_select_read()
  - enif_select_write()
  - enif_self()
  - enif_send()
  - enif_set_pid_undefined()
  - enif_sizeof_resource()
  - enif_snprintf()
  - enif_system_info()
  - enif_term_to_binary()
  - enif_term_type()
  - enif_thread_create()
  - enif_thread_exit()
  - enif_thread_join()
  - enif_thread_name()
  - enif_thread_opts_create()
  - enif_thread_opts_destroy()
  - enif_thread_self()
  - enif_thread_type()
  - enif_time_offset()
  - enif_tsd_get()
  - enif_tsd_key_create()
  - enif_tsd_key_destroy()
  - enif_tsd_set()
  - enif_vfprintf()
  - enif_vsnprintf()
  - enif_whereis_pid()
  - enif_whereis_port()
- erl_prim_loader
  - Top of manual page
  - get_file/1
  - get_path/0
  - list_dir/1
  - read_file_info/1
  - read_link_info/1
  - set_path/1
- erlsrv
- erl_tracer
  - Top of manual page
  - Module:enabled/3
  - Module:enabled_call/3
  - Module:enabled_garbage_collection/3
  - Module:enabled_ports/3
  - Module:enabled_procs/3
  - Module:enabled_receive/3
  - Module:enabled_running_ports/3
  - Module:enabled_running_procs/3
  - Module:enabled_send/3
  - Module:trace/5
  - Module:trace_call/5
  - Module:trace_garbage_collection/5
  - Module:trace_ports/5
  - Module:trace_procs/5
  - Module:trace_receive/5
  - Module:trace_running_ports/5
  - Module:trace_running_procs/5
  - Module:trace_send/5
- erts_alloc
- escript
- init
  - Top of manual page
  - boot/1
  - get_argument/1
  - get_arguments/0
  - get_plain_arguments/0
  - get_status/0
  - reboot/0
  - restart/0
  - script_id/0
  - stop/0
  - stop/1
- persistent_term
  - Top of manual page
  - erase/1
  - get/0
  - get/1
  - get/2
  - info/0
  - put/2
- run_erl
- socket
  - Top of manual page
  - accept/1
  - accept/2
  - bind/2
  - cancel/2
  - close/1
  - connect/2
  - connect/3
  - getopt/3
  - info/1
  - listen/1
  - listen/2
  - number_of/0
  - open/2
  - open/3
  - open/4
  - peername/1
  - recv/1
  - recv/2
  - recv/3
  - recv/4
  - recvfrom/1
  - recvfrom/2
  - recvfrom/3
  - recvfrom/4
  - recvmsg/1
  - recvmsg/2
  - recvmsg/3
  - recvmsg/5
  - send/2
  - send/3
  - send/4
  - sendmsg/2
  - sendmsg/3
  - sendmsg/4
  - sendto/3
  - sendto/4
  - sendto/5
  - setopt/4
  - shutdown/2
  - sockname/1
  - supports/0
  - supports/1
  - supports/2
  - supports/3
  - which_sockets/0
  - which_sockets/1
- start
- start_erl
- werl
- zlib
  - Top of manual page
  - adler32/2
  - adler32/3
  - adler32_combine/4
  - close/1
  - compress/1
  - crc32/1
  - crc32/2
  - crc32/3
  - crc32_combine/4
  - deflate/2
  - deflate/3
  - deflateEnd/1
  - deflateInit/1
  - deflateInit/2
  - deflateInit/6
  - deflateParams/3
  - deflateReset/1
  - deflateSetDictionary/2
  - getBufSize/1
  - gunzip/1
  - gzip/1
  - inflate/2
  - inflate/3
  - inflateChunk/1
  - inflateChunk/2
  - inflateEnd/1
  - inflateGetDictionary/1
  - inflateInit/1
  - inflateInit/2
  - inflateReset/1
  - inflateSetDictionary/2
  - open/0
  - safeInflate/2
  - setBufSize/2
  - set_controlling_process/2
  - uncompress/1
  - unzip/1
  - zip/1

# escript

### Command

escript

### Command Summary

Erlang scripting support

### Description

escript provides support for running short Erlang programs without having to compile them first, and an easy way to retrieve the command-line arguments.

It is possible to bundle escript(s) with an Erlang runtime system to make it self-sufficient and relocatable. In such a standalone system, the escript(s) should be located in the top bin directory of the standalone system and given .escript as file extension. Further the (built-in) escript program should be copied to the same directory and given the scripts original name (without the .escript extension). This will enable use of the bundled Erlang runtime system.

The (built-in) escript program first determines which Erlang runtime system to use and then starts it to execute your script. Usually the runtime system is located in the same Erlang installation as the escript program itself. But for standalone systems with one or more escripts it may be the case that the escript program in your path actually starts the runtime system bundled with the escript. This is intentional, and typically happens when the standalone system bin directory is not in the execution path (as it may cause its erl program to override the desired one) and the escript(s) are referred to via symbolic links from a bin directory in the path.

### Exports

script-name script-arg1 script-arg2...
escript escript-flags script-name script-arg1 script-arg2...

escript runs a script written in Erlang.

Example:

    $ chmod u+x factorial
    $ cat factorial
    #!/usr/bin/env escript
    %% -*- erlang -*-
    %%! -smp enable -sname factorial -mnesia debug verbose
    main([String]) ->
        try
            N = list_to_integer(String),
            F = fac(N),
            io:format("factorial ~w = ~w\n", [N,F])
        catch
            _:_ ->
                usage()
        end;
    main(_) ->
        usage().

    usage() ->
        io:format("usage: factorial integer\n"),
        halt(1).

    fac(0) -> 1;
    fac(N) -> N * fac(N-1).
    $ ./factorial 5
    factorial 5 = 120
    $ ./factorial
    usage: factorial integer
    $ ./factorial five
    usage: factorial integer

The header of the Erlang script in the example differs from a normal Erlang module. The first line is intended to be the interpreter line, which invokes escript.

However, if you invoke the escript as follows, the contents of the first line does not matter, but it cannot contain Erlang code as it will be ignored:

    $ escript factorial 5

The second line in the example contains an optional directive to the Emacs editor, which causes it to enter the major mode for editing Erlang source files. If the directive is present, it must be located on the second line.

If a comment selecting the encoding exists, it can be located on the second line.

Note

The encoding specified by the above mentioned comment applies to the script itself. The encoding of the I/O-server, however, must be set explicitly as follows:

    io:setopts([{encoding, unicode}])

The default encoding of the I/O-server for standard_io is latin1, as the script runs in a non-interactive terminal (see section Summary of Options) in the STDLIB User's Guide.

On the third line (or second line depending on the presence of the Emacs directive), arguments can be specified to the emulator, for example:

    %%! -smp enable -sname factorial -mnesia debug verbose

Such an argument line must start with %%! and the remaining line is interpreted as arguments to the emulator.

If you know the location of the escript executable, the first line can directly give the path to escript, for example:

    #!/usr/local/bin/escript

As any other type of scripts, Erlang scripts do not work on Unix platforms if the execution bit for the script file is not set. (To turn on the execution bit, use chmod +x script-name.)

The remaining Erlang script file can either contain Erlang source code, an inlined beam file, or an inlined archive file.

An Erlang script file must always contain the main/1 function. When the script is run, the main/1 function is called with a list of strings representing the arguments specified to the script (not changed or interpreted in any way).

If the main/1 function in the script returns successfully, the exit status for the script is 0. If an exception is generated during execution, a short message is printed and the script terminates with exit status 127.

To return your own non-zero exit code, call halt(ExitCode), for example:

    halt(1).

To retrieve the pathname of the script, call escript:script_name() from your script (the pathname is usually, but not always, absolute).

If the file contains source code (as in the example above), it is processed by the epp preprocessor. This means that you, for example, can use predefined macros (such as ?MODULE) and include directives like the -include_lib directive. For example, use

    -include_lib("kernel/include/file.hrl").

to include the record definitions for the records used by function file:read_link_info/1. You can also select encoding by including an encoding comment here, but if a valid encoding comment exists on the second line, it takes precedence.

The script is checked for syntactic and semantic correctness before it is run. If there are warnings (such as unused variables), they are printed and the script will still be run. If there are errors, they are printed and the script will not be run and its exit status is 127.

Both the module declaration and the export declaration of the main/1 function are optional.

By default, the script will be interpreted. You can force it to be compiled by including the following line somewhere in the script file:

    -mode(compile).

Execution of interpreted code is slower than compiled code. If much of the execution takes place in interpreted code, it can be worthwhile to compile it, although the compilation itself takes a little while. Also, native can be supplied instead of compile. This compiles the script using the native flag and may or may not be worthwhile depending on the escript characteristics.

As mentioned earlier, a script can contains precompiled beam code. In a precompiled script, the interpretation of the script header is the same as in a script containing source code. This means that you can make a beam file executable by prepending the file with the lines starting with #! and %%! mentioned above. In a precompiled script, the main/1 function must be exported.

Another option is to have an entire Erlang archive in the script. In an archive script, the interpretation of the script header is the same as in a script containing source code. This means that you can make an archive file executable by prepending the file with the lines starting with #! and %%! mentioned above. In an archive script, the main/1 function must be exported. By default the main/1 function in the module with the same name as the basename of the escript file is invoked. This behavior can be overridden by setting flag -escript main Module as one of the emulator flags. Module must be the name of a module that has an exported main/1 function. For more information about archives and code loading, see code(3).

It is often very convenient to have a header in the escript, especially on Unix platforms. However, the header is optional, so you directly can "execute" an Erlang module, Beam file, or archive file without adding any header to them. But then you have to invoke the script as follows:

    $ escript factorial.erl 5
    factorial 5 = 120
    $ escript factorial.beam 5
    factorial 5 = 120
    $ escript factorial.zip 5
    factorial 5 = 120

escript:create(FileOrBin, Sections) -> ok | {ok, binary()} | {error, term()}

### Types

FileOrBin = filename() | 'binary'
Sections = [Header] Body | Body
Header = shebang | {shebang, Shebang} | comment | {comment, Comment} | {emu_args, EmuArgs}
Shebang = string() | 'default' | 'undefined'
Comment = string() | 'default' | 'undefined'
EmuArgs = string() | 'undefined'
Body = {source, SourceCode} | {beam, BeamCode} | {archive, ZipArchive} | {archive, ZipFiles, ZipOptions}
SourceCode = BeamCode = file:filename() | binary()
ZipArchive = zip:filename() | binary()
ZipFiles = [ZipFile]
ZipFile = file:filename() | {file:filename(), binary()} | {file:filename(), binary(), file:file_info()}
ZipOptions = [zip:create_option()]

Creates an escript from a list of sections. The sections can be specified in any order. An escript begins with an optional Header followed by a mandatory Body. If the header is present, it does always begin with a shebang, possibly followed by a comment and emu_args. The shebang defaults to "/usr/bin/env escript". The comment defaults to "This is an -*- erlang -*- file". The created escript can either be returned as a binary or written to file.

As an example of how the function can be used, we create an interpreted escript that uses emu_args to set some emulator flag. In this case, it happens to disable the smp_support. We also extract the different sections from the newly created script:

    > Source = "%% Demo\nmain(_Args) ->\n    io:format(erlang:system_info(smp_support)).\n".
    "%% Demo\nmain(_Args) ->\n    io:format(erlang:system_info(smp_support)).\n"
    > io:format("~s\n", [Source]).
    %% Demo
    main(_Args) ->
        io:format(erlang:system_info(smp_support)).

    ok
    > {ok, Bin} = escript:create(binary, [shebang, comment, {emu_args, "-smp disable"},
                                          {source, list_to_binary(Source)}]).
    {ok,<<"#!/usr/bin/env escript\n%% This is an -*- erlang -*- file\n%%!-smp disabl"...>>}
    > file:write_file("demo.escript", Bin).
    ok
    > os:cmd("escript demo.escript").
    "false"
    > escript:extract("demo.escript", []).
    {ok,[{shebang,default}, {comment,default}, {emu_args,"-smp disable"},
         {source,<<"%% Demo\nmain(_Args) ->\n    io:format(erlang:system_info(smp_su"...>>}]}

An escript without header can be created as follows:

    > file:write_file("demo.erl",
                      ["%% demo.erl\n-module(demo).\n-export([main/1]).\n\n", Source]).
    ok
    > {ok, _, BeamCode} = compile:file("demo.erl", [binary, debug_info]).
    {ok,demo,
        <<70,79,82,49,0,0,2,208,66,69,65,77,65,116,111,109,0,0,0,
          79,0,0,0,9,4,100,...>>}
    > escript:create("demo.beam", [{beam, BeamCode}]).
    ok
    > escript:extract("demo.beam", []).
    {ok,[{shebang,undefined}, {comment,undefined}, {emu_args,undefined},
         {beam,<<70,79,82,49,0,0,3,68,66,69,65,77,65,116,
                 111,109,0,0,0,83,0,0,0,9,...>>}]}
    > os:cmd("escript demo.beam").
    "true"

Here we create an archive script containing both Erlang code and Beam code, then we iterate over all files in the archive and collect their contents and some information about them:

    > {ok, SourceCode} = file:read_file("demo.erl").
    {ok,<<"%% demo.erl\n-module(demo).\n-export([main/1]).\n\n%% Demo\nmain(_Arg"...>>}
    > escript:create("demo.escript",
                     [shebang,
                      {archive, [{"demo.erl", SourceCode},
                                 {"demo.beam", BeamCode}], []}]).
    ok
    > {ok, [{shebang,default}, {comment,undefined}, {emu_args,undefined},
         {archive, ArchiveBin}]} = escript:extract("demo.escript", []).
    {ok,[{shebang,default}, {comment,undefined}, {emu_args,undefined},
         {{archive,<<80,75,3,4,20,0,0,0,8,0,118,7,98,60,105,
                    152,61,93,107,0,0,0,118,0,...>>}]}
    > file:write_file("demo.zip", ArchiveBin).
    ok
    > zip:foldl(fun(N, I, B, A) -> [{N, I(), B()} | A] end, [], "demo.zip").
    {ok,[{"demo.beam",
          {file_info,748,regular,read_write,
                     {{2010,3,2},{0,59,22}},
                     {{2010,3,2},{0,59,22}},
                     {{2010,3,2},{0,59,22}},
                     54,1,0,0,0,0,0},
          <<70,79,82,49,0,0,2,228,66,69,65,77,65,116,111,109,0,0,0,
            83,0,0,...>>},
         {"demo.erl",
          {file_info,118,regular,read_write,
                     {{2010,3,2},{0,59,22}},
                     {{2010,3,2},{0,59,22}},
                     {{2010,3,2},{0,59,22}},
                     54,1,0,0,0,0,0},
          <<"%% demo.erl\n-module(demo).\n-export([main/1]).\n\n%% Demo\nmain(_Arg"...>>}]}

escript:extract(File, Options) -> {ok, Sections} | {error, term()}

### Types

File = filename()
Options = [] | [compile_source]
Sections = Headers Body
Headers = {shebang, Shebang} {comment, Comment} {emu_args, EmuArgs}
Shebang = string() | 'default' | 'undefined'
Comment = string() | 'default' | 'undefined'
EmuArgs = string() | 'undefined'
Body = {source, SourceCode} | {source, BeamCode} | {beam, BeamCode} | {archive, ZipArchive}
SourceCode = BeamCode = ZipArchive = binary()

Parses an escript and extracts its sections. This is the reverse of create/2.

All sections are returned even if they do not exist in the escript. If a particular section happens to have the same value as the default value, the extracted value is set to the atom default. If a section is missing, the extracted value is set to the atom undefined.

Option compile_source only affects the result if the escript contains source code. In this case the Erlang code is automatically compiled and {source, BeamCode} is returned instead of {source, SourceCode}.

Example:

    > escript:create("demo.escript",
                     [shebang, {archive, [{"demo.erl", SourceCode},
                                          {"demo.beam", BeamCode}], []}]).
    ok
    > {ok, [{shebang,default}, {comment,undefined}, {emu_args,undefined},
         {archive, ArchiveBin}]} =
                  escript:extract("demo.escript", []).
    {ok,[{{archive,<<80,75,3,4,20,0,0,0,8,0,118,7,98,60,105,
                    152,61,93,107,0,0,0,118,0,...>>}
         {emu_args,undefined}]}

escript:script_name() -> File

### Types

File = filename()

Returns the name of the escript that is executed. If the function is invoked outside the context of an escript, the behavior is undefined.

### Options Accepted By escript

-c
  Compiles the escript regardless of the value of the mode attribute.
-d
  Debugs the escript. Starts the debugger, loads the module containing the main/1 function into the debugger, sets a breakpoint in main/1, and invokes main/1. If the module is precompiled, it must be explicitly compiled with option debug_info.
-i
  Interprets the escript regardless of the value of the mode attribute.
-s
  Performs a syntactic and semantic check of the script file. Warnings and errors (if any) are written to the standard output, but the script will not be run. The exit status is 0 if any errors are found, otherwise 127.
-n
  Compiles the escript using flag +native.