{"id":22886,"date":"2023-02-19T00:51:38","date_gmt":"2023-02-18T19:21:38","guid":{"rendered":"https:\/\/java2blog.com\/?p=22886"},"modified":"2023-02-19T00:51:38","modified_gmt":"2023-02-18T19:21:38","slug":"echo-1-bash","status":"publish","type":"post","link":"https:\/\/java2blog.com\/echo-1-bash\/","title":{"rendered":"What is echo $1 in Bash"},"content":{"rendered":"<h2>Meaning of  the <code>echo $1<\/code> in Bash<\/h2>\n<p>The <code>echo $1<\/code> is a bash command; now, those new to Bash can think of it as a famous shell and scripting language in Unix-based operating systems. Using Bash, we can take advantage of multiple features and commands that assist users in automation and system administration tasks. <\/p>\n<p>Are you comfortable with Bash? Great, let&#8217;s continue with <code>echo $1<\/code>. First, the <code>echo<\/code> command displays messages and outcomes on the console. For instance, if we want to show <code>Hi<\/code>, we will use <code>echo &quot;Hi&quot;<\/code> and hit <kbd>Enter<\/kbd>.<\/p>\n<p>This way, <code>Hi<\/code> will be printed on the console. Now, what does <code>$1<\/code> means? In Bash, <code>$1<\/code> represents the first argument passed to the function or a bash script. So, if you have multiple arguments to pass, then <code>$1<\/code> will mean the first argument, <code>$2<\/code> will represent the second argument, and so on.<\/p>\n<p>Combining <code>echo<\/code> and <code>$1<\/code> as <code>echo $1<\/code> means printing or displaying the first argument&#8217;s value passed to the particular function or a script. Let&#8217;s understand the use of <code>echo $1<\/code> with a script and function.<\/p>\n<h3>Use <code>if-else<\/code> in Bash Script to Demonstrate <code>echo $1<\/code><\/h3>\n<p><strong>Use <code>if-else<\/code> in Bash Script to Demonstrate the use of the <code>echo $1<\/code> command.<\/strong><\/p>\n<pre code=\"bash\" title=\"Use echo $1 in Bash Script\">\n#!\/bin\/bash\nif [ -z \"$1\" ]; then\n  echo \"Error: You didn't provide any argument.\"\nelif [ $1 -gt 0 ]; then\n  echo \"$1 is the positive number.\"\nelif [ $1 -lt 0 ]; then\n  echo \"$1 is the negative number.\"\nelse\n  echo \"$1 is zero.\"\nfi\n<\/pre>\n<p>The output will differ based on the given arguments or no arguments. Let&#8217;s run the above file as <code>.\/test.sh 9<\/code> to get an outcome.<\/p>\n<pre title=\"OUTPUT\">\n9 is the positive number.\n<\/pre>\n<p>In the provided script above, we used <code>if-else<\/code> blocks to check the first argument&#8217;s value, whether it is a positive number, negative number, zero, or not provided. Here, we used various expressions and operators to check the value of the <code>$1<\/code>.<\/p>\n<p>First, the <code>-z<\/code> option was used to check if the variable was empty, so <code>[-z &quot;$1&quot;]<\/code> was used to check if the first command line argument was an empty string. Next, <code>[ $1 -gt 0 ]<\/code> was used to check if the value of the first command-line argument was greater than 0. Here, <code>-gt<\/code> is a comparison operator meaning <code>greater than<\/code>.<\/p>\n<p>Finally, <code>[ $1 -lt 0 ]<\/code> was used to check if the value of the first command-line argument is less than 0. Here, <code>-lt<\/code> is a comparison operator meaning <code>less than<\/code>.<\/p>\n<blockquote>\n<p>Only one block will be executed at a time, whether it is <code>if<\/code>, <code>elif<\/code> or <code>else<\/code>.<\/p>\n<\/blockquote>\n<h3>Use User-Defined Function to Demonstrate <code>echo $1<\/code><\/h3>\n<p><strong>Use user-defined functions using the console to learn how to use <code>echo $1<\/code> in Bash.<\/strong><\/p>\n<pre code=\"bash\" title=\"Use echo $1 in Function\">\nfunction printFirstArgument {\n    echo \"The first argument you provided is: $1\"\n}\nprintFirstArgument \"Hello, how are you?\"\n<\/pre>\n<pre title=\"OUTPUT\">\nThe first argument you provided is: Hello, how are you?\n<\/pre>\n<p>Here, we defined a function named <code>printFirstArgument<\/code> to whom we passed <code>Hello, how are you?<\/code> as the first command-line argument, which was printed due to using <code>echo $1<\/code> as you can inside the function body. You can also write this function in a bash script and run that script by passing the same argument.<\/p>\n<p>That&#8217;s all about echo $1 in Bash.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Meaning of the echo $1 in Bash The echo $1 is a bash command; now, those new to Bash can think of it as a famous shell and scripting language in Unix-based operating systems. Using Bash, we can take advantage of multiple features and commands that assist users in automation and system administration tasks. Are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":22964,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[161],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/22886"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=22886"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/22886\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/22964"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=22886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=22886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=22886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}