{"id":33494,"date":"2019-10-31T21:52:52","date_gmt":"2019-10-31T18:52:52","guid":{"rendered":"https:\/\/prohoster.info\/blog\/sem-neozhidannyh-peremennyh-bash\/"},"modified":"2019-10-31T21:52:52","modified_gmt":"2019-10-31T18:52:52","slug":"sem-neozhidannyh-peremennyh-bash","status":"publish","type":"post","link":"https:\/\/prohoster.info\/it\/blog\/administrirovanie\/sem-neozhidannyh-peremennyh-bash","title":{"rendered":"Sette variabili Bash inaspettate","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p>Continuando la serie di appunti su <noindex><a rel=\"nofollow\" href=\"https:\/\/zwischenzugs.com\/2019\/04\/03\/eight-obscure-bash-options-you-might-want-to-know-about\/\">meno conosciuti<\/a><\/noindex> <noindex><a rel=\"nofollow\" href=\"https:\/\/zwischenzugs.com\/2019\/02\/27\/bash-startup-explained\/\">funzioni<\/a><\/noindex> bash, vi mostrer\u00f2 sette variabili di cui potreste non essere a conoscenza.<\/p>\n<h4>1) <code>PROMPT_COMMAND<\/code><\/h4>\n<p>\n\u00c8 probabile che sappiate gi\u00e0 come maneggiare il prompt per mostrare varie informazioni utili, ma non tutti sanno che ogni volta che viene mostrato il prompt si pu\u00f2 eseguire un comando della shell.<\/p>\n<p>Infatti, molti complessi manipolatori di prompt utilizzano questa variabile per eseguire comandi che raccolgono informazioni da visualizzare nel prompt.<\/p>\n<p>Provate a eseguire questo in una nuova shell e vedrete cosa succede alla sessione:<\/p>\n<pre><code class=\"bash\">$ PROMPT_COMMAND='echo -n \"scrivendo il prompt a \" &amp;&amp; date'<\/code><\/pre>\n<p><noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<h4>2) <code>HISTTIMEFORMAT<\/code><\/h4>\n<p>\nSe si esegue <code>la cronologia<\/code> nella console, otterrete un elenco dei comandi precedentemente eseguiti con il vostro account.<\/p>\n<pre><code class=\"bash\">$ HISTTIMEFORMAT='I ran this at: %d\/%m\/%y %T '<\/code><\/pre>\n<p>\nUna volta impostata questa variabile, i nuovi registri scrivono l'ora insieme al comando, quindi l'output apparir\u00e0 cos\u00ec:<\/p>\n<pre>1871  I ran this at: 01\/05\/19 13:38:07 cat \/etc\/resolv.conf\n1872  I ran this at: 01\/05\/19 13:38:19 curl bbc.co.uk\n1873  I ran this at: 01\/05\/19 13:38:41 sudo vi \/etc\/resolv.conf\n1874  I ran this at: 01\/05\/19 13:39:18 curl -vvv bbc.co.uk\n1876  I ran this at: 01\/05\/19 13:39:25 sudo su -<\/pre>\n<p>\nLa formattazione corrisponde ai simboli di <code>man date<\/code>.<\/p>\n<h4>3) <code>CDPATH<\/code><\/h4>\n<p>\nPer risparmiare tempo nella riga di comando, puoi usare questa variabile per cambiare directory tanto facilmente quanto invochi comandi.<\/p>\n<p>Come <code>PATH<\/code>, la variabile <code>CDPATH<\/code> \u00e8 un elenco di percorsi separati da due punti. Quando esegui un comando <code>cd<\/code> con un percorso relativo (ossia senza barra all'inizio), di default la shell cerca nella tua cartella locale nomi corrispondenti. <code>CDPATH<\/code> cercer\u00e0 nei percorsi che hai fornito per la directory in cui desideri andare.<\/p>\n<p>Se imposti <code>CDPATH<\/code> in questo modo:<\/p>\n<pre><code class=\"bash\">$ CDPATH=\/:\/lib<\/code><\/pre>\n<p>\ne poi inserisci:<\/p>\n<pre><code class=\"bash\">$ cd \/home\n$ cd tmp<\/code><\/pre>\n<p>\nsarai sempre indirizzato a <code>\/tmp<\/code> indipendentemente da dove ti trovi.<\/p>\n<p>Ma fai attenzione, perch\u00e9 se non specifichi nella lista la cartella locale (<code>.<\/code>) non potrai creare qualsiasi altra cartella <code>tmp<\/code> e accedervi come al solito:<\/p>\n<pre><code class=\"bash\">$ cd \/home\n$ mkdir tmp\n$ cd tmp\n$ pwd\n\/tmp<\/code><\/pre>\n<p>\nOps!<\/p>\n<p>Questo sembra il malinteso che ho provato quando ho capito che la cartella locale non era stata inclusa nella variabile pi\u00f9 familiare <code>PATH<\/code>\u2026 ma dovresti farlo nella variabile PATH, perch\u00e9 potresti essere ingannato ad eseguire un comando falso da qualche codice scaricato.<\/p>\n<p>Il mio viene impostato come punto iniziale:<\/p>\n<pre><code class=\"bash\">CDPATH=.:\/space:\/etc:\/var\/lib:\/usr\/share:\/opt<\/code><\/pre>\n<p><\/p>\n<h4>4) <code>SHLVL<\/code><\/h4>\n<p>\nTi sei mai chiesto, inserendo <code>exit<\/code> ti porter\u00e0 fuori dall'attuale shell bash in un'altra shell \"padre\" oppure semplicemente chiuder\u00e0 completamente la finestra del terminale?<\/p>\n<p>Questa variabile tiene traccia di quanto profondamente sei immerso nella shell bash. Se crei un nuovo terminale, \u00e8 impostata su 1:<\/p>\n<pre><code class=\"bash\">$ echo $SHLVL\n1<\/code><\/pre>\n<p>\nPoi, se avvii un altro processo della shell, il numero aumenta:<\/p>\n<pre><code class=\"bash\">$ bash\n$ echo $SHLVL\n2<\/code><\/pre>\n<p>\nQuesto pu\u00f2 essere molto utile negli script, dove non sei sicuro se uscire o meno, o per tenere traccia di dove ti trovi in termini di nidificazione.<\/p>\n<h4>5) <code>LINENO<\/code><\/h4>\n<p>\nAnche per analizzare lo stato attuale e per il debug, la variabile <code>LINENO<\/code>, che riporta il numero di comandi eseguiti nella sessione fino a questo momento:<\/p>\n<pre><code class=\"bash\">$ bash\n$ echo $LINENO\n1\n$ echo $LINENO\n2<\/code><\/pre>\n<p>\nQuesto \u00e8 utilizzato pi\u00f9 frequentemente durante il debug degli script. Inserendo righe come <code>echo DEBUG:$LINENO<\/code>, puoi rapidamente determinare dove ti trovi nello script (o meno).<\/p>\n<h4>6) <code>REPLY<\/code><\/h4>\n<p>\nSe, come me, di solito scrivi codice del genere:<\/p>\n<pre><code class=\"bash\">$ read input\necho fai qualcosa con $input<\/code><\/pre>\n<p>\npu\u00f2 essere una sorpresa che non devi preoccuparti affatto di creare una variabile:<\/p>\n<pre><code class=\"bash\">$ read\necho fai qualcosa con $REPLY<\/code><\/pre>\n<p>\nQuesto fa la stessa cosa.<\/p>\n<h4>7) <code>TMOUT<\/code><\/h4>\n<p>\nPer non rimanere sui server di produzione troppo a lungo per motivi di sicurezza o per non avviare accidentalmente qualcosa di pericoloso nel terminale sbagliato, impostare questa variabile funge da protezione.<\/p>\n<p>Se non viene inserito nulla per il numero stabilito di secondi, si esce dalla shell.<\/p>\n<p>Cio\u00e8, \u00e8 un'alternativa a <code>sleep 1 &amp;&amp; exit<\/code>:<\/p>\n<pre><code class=\"bash\">$ TMOUT=1<\/code><\/pre>\n<p>Fonte: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/post\/451492\/\">habr.com<\/a><\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044f \u0441\u0435\u0440\u0438\u044e \u0437\u0430\u043c\u0435\u0442\u043e\u043a \u043e \u043c\u0435\u043d\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 bash, \u043f\u043e\u043a\u0430\u0436\u0443 \u0432\u0430\u043c \u0441\u0435\u043c\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u043d\u0435 \u0437\u043d\u0430\u0442\u044c. 1) PROMPT_COMMAND \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u044b \u0443\u0436\u0435 \u0432 \u043a\u0443\u0440\u0441\u0435, \u043a\u0430\u043a \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0435\u043d\u0438\u0435\u043c prompt, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u0443\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u043d\u043e \u043d\u0435 \u0432\u0441\u0435 \u0437\u043d\u0430\u044e\u0442, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u043f\u043e\u043a\u0430\u0437\u0435 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0438. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0442\u043e\u0440\u044b prompt [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[688],"tags":[],"class_list":["post-33494","post","type-post","status-publish","format-standard","hentry","category-administrirovanie"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044f \u0441\u0435\u0440\u0438\u044e \u0437\u0430\u043c\u0435\u0442\u043e\u043a \u043e \u043c\u0435\u043d\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/it\/blog\/administrirovanie\/sem-neozhidannyh-peremennyh-bash\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"it_IT\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47\u0421\u0435\u043c\u044c \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 Bash | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044f \u0441\u0435\u0440\u0438\u044e \u0437\u0430\u043c\u0435\u0442\u043e\u043a \u043e \u043c\u0435\u043d\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/it\/blog\/administrirovanie\/sem-neozhidannyh-peremennyh-bash\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2019-10-31T18:52:52+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2019-10-31T18:52:52+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47Sette variabili Bash inaspettate | ProHoster","description":"Continuando la serie di appunti su variabili meno conosciute","canonical_url":"https:\/\/prohoster.info\/it\/blog\/administrirovanie\/sem-neozhidannyh-peremennyh-bash","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"it_IT","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47\u0421\u0435\u043c\u044c \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 Bash | ProHoster","og:description":"\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044f \u0441\u0435\u0440\u0438\u044e \u0437\u0430\u043c\u0435\u0442\u043e\u043a \u043e \u043c\u0435\u043d\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445","og:url":"https:\/\/prohoster.info\/it\/blog\/administrirovanie\/sem-neozhidannyh-peremennyh-bash","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2019-10-31T18:52:52+00:00","article:modified_time":"2019-10-31T18:52:52+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"33494","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":"2026-01-21 15:30:22","breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-03-01 02:39:23","updated":"2026-01-21 15:30:22","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/posts\/33494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/comments?post=33494"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/posts\/33494\/revisions"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/media?parent=33494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/categories?post=33494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/tags?post=33494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}