{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Shell commands in IPython" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The IPython Notebook allows simple UNIX/Linux commands to be executed in a single input cell. There are no limits but when using, please keep in mind that in contrast to a regular UNIX/Linux shell, start each shell command with a `!`, for example `!ls` for the command `ls` (see below for further explanations about the command). Furthermore, each shell command is executed in its own subshell. For this reason, the results of previous shell commands are not available to you.\n", "\n", "To begin with, the command `ls` lists the files in the current working directory. The output is shown below the input cell, and lists the single file `shell.ipynb`:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "debugging.ipynb \u001b[34mmypackage\u001b[m\u001b[m\n", "debugging.ipynb.license myscript.py\n", "display.ipynb shell.ipynb\n", "display.ipynb.license shell.ipynb.license\n", "examples.ipynb start.rst\n", "examples.ipynb.license \u001b[31mtab-completion-for-anything.png\u001b[m\u001b[m\n", "extensions.rst tab-completion-for-anything.png.license\n", "importing.ipynb \u001b[31mtab-completion-for-modules.png\u001b[m\u001b[m\n", "importing.ipynb.license tab-completion-for-modules.png.license\n", "index.rst \u001b[31mtab-completion-for-objects.png\u001b[m\u001b[m\n", "magics.ipynb tab-completion-for-objects.png.license\n", "magics.ipynb.license \u001b[34munix-shell\u001b[m\u001b[m\n" ] } ], "source": [ "!ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The command `!pwd` displays the path to working directory:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython\n" ] } ], "source": [ "!pwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The command `!echo` outputs text given as parameter to the `echo` command. The example below demonstrates how to print `Hello world`:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world!\n" ] } ], "source": [ "!echo \"Hello world!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Passing values to and from the shell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is a clever way through which you can access the output of a UNIX/Linux command as a variable in Python. Assign the output of a UNIX/Linux command to a variable as follows:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "contents = !ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here the Python variable `contents` has been assigned the output of the command `ls`. As a result, `contents` is a list, where each list element corresponds to a line in the output. With the `print` command you output the list contents:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['debugging.ipynb', 'debugging.ipynb.license', 'display.ipynb', 'display.ipynb.license', 'examples.ipynb', 'examples.ipynb.license', 'extensions.rst', 'importing.ipynb', 'importing.ipynb.license', 'index.rst', 'magics.ipynb', 'magics.ipynb.license', '\\x1b[34mmypackage\\x1b[m\\x1b[m', 'myscript.py', 'shell.ipynb', 'shell.ipynb.license', 'start.rst', '\\x1b[31mtab-completion-for-anything.png\\x1b[m\\x1b[m', 'tab-completion-for-anything.png.license', '\\x1b[31mtab-completion-for-modules.png\\x1b[m\\x1b[m', 'tab-completion-for-modules.png.license', '\\x1b[31mtab-completion-for-objects.png\\x1b[m\\x1b[m', 'tab-completion-for-objects.png.license', '\\x1b[34munix-shell\\x1b[m\\x1b[m']\n" ] } ], "source": [ "print(contents)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will see the same result below when executing the pwd command. The current directory is stored in the variable directory:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "directory = !pwd" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['/Users/veit/cusy/trn/Python4DataScience/docs/workspace/ipython']\n" ] } ], "source": [ "print(directory)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }