{"id":2376,"date":"2010-07-09T08:45:00","date_gmt":"2010-07-09T08:45:00","guid":{"rendered":"http:\/\/www.b.shuttle.de\/hayek\/Hayek\/Jochen\/wp\/blog-en\/2010\/07\/09\/best-practices-in-shell-script-programming-double-quotes\/"},"modified":"2010-07-09T08:45:00","modified_gmt":"2010-07-09T08:45:00","slug":"best-practices-in-shell-script-programming-double-quotes","status":"publish","type":"post","link":"https:\/\/wp.jochen.hayek.name\/blog-en\/2010\/07\/09\/best-practices-in-shell-script-programming-double-quotes\/","title":{"rendered":"best practices in shell script programming \u2013 double quotes"},"content":{"rendered":"<p>\nYou do know the result of this:<\/p>\n<blockquote><p>\n<span>a=A; echo $a<\/span><\/p><\/blockquote>\n<div>\nBut are you just as sure here?<\/div>\n<div>\n<\/div>\n<blockquote><p>\n<span>a=A; b=B; echo $a.$b<\/span><\/p><\/blockquote>\n<div>\nThat depends on the shell, you are using. So I suggest you better write it this way:<\/div>\n<p><\/p>\n<blockquote><p>\n<span>a=A; b=B; echo ${a}.${b}<\/span><\/p><\/blockquote>\n<div>\nEnclosing variable names in curly braces is quite often a good idea.<\/div>\n<div>\n<\/div>\n<div>\nLook at the following piece of code:<\/div>\n<blockquote><p>\n<span><span>case $var in<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0x*)<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0 \u00a0echo var starts with x<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0 \u00a0;;<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0*)<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0 \u00a0echo var starts with something else<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0 \u00a0;;<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>esac<\/span><\/span><\/p><\/blockquote>\n<div>\nLooks alright, doesn&#8217;t it?<\/div>\n<div>\nNo, it&#8217;s does not. If that variable had not been assigned a value before or is of zero length, you will see an ugly syntax error occur.<\/div>\n<div>\nTherefore enclose the variable in double quotes like here:<\/div>\n<div>\n<blockquote><p>\n<span><span>case &#8220;$var&#8221; in<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0x*)<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0 \u00a0echo var starts with x<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0 \u00a0;;<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0*)<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0 \u00a0echo var starts with something else<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>\u00a0\u00a0 \u00a0;;<\/span><\/span><\/p><\/blockquote>\n<blockquote><p>\n<span><span>esac<\/span><\/span><\/p><\/blockquote>\n<\/div>\n<div>\n<span><span>There is no good excuse for not doing it anywhere. It may look ugly and unnecessary, but it will help. That&#8217;s the way shell scripting is. I learned this from <a href=\"http:\/\/www.oreillynet.com\/pub\/au\/2320\">J\u00fcrgen Gulbins<\/a> around 1987, when I enjoyed working for him. This series of articles is dedicated to him. I owe him a lot.<\/span><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You do know the result of this: a=A; echo $a But are you just as sure here? a=A; b=B; echo $a.$b That depends on the shell, you are using. So I suggest you better write it this way: a=A; b=B; echo ${a}.${b} Enclosing variable names in curly braces is quite often a good idea. Look [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"0"},"categories":[666],"tags":[],"class_list":["post-2376","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paO0kP-Ck","jetpack_likes_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts\/2376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/comments?post=2376"}],"version-history":[{"count":0,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts\/2376\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/media?parent=2376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/categories?post=2376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/tags?post=2376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}