{"id":38,"date":"2024-02-18T23:14:37","date_gmt":"2024-02-18T23:14:37","guid":{"rendered":"http:\/\/courses.usnh.domains\/cms\/?p=38"},"modified":"2024-02-18T23:14:37","modified_gmt":"2024-02-18T23:14:37","slug":"input","status":"publish","type":"post","link":"https:\/\/courses.usnh.domains\/cms\/it-612\/input\/","title":{"rendered":"Input"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Bash User Input<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With the previous script, we defined a variable, and we output the value of the variable on the screen with the <code>echo $name<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s go ahead and ask the user for input instead. To do that again, open the file with your favorite text editor and update the script as follows: #!\/bin\/bash echo &#8220;What is your name?&#8221; read name echo &#8220;Hi there $name&#8221; echo &#8220;Welcome to DevDojo!&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above will prompt the user for input and then store that input as a string\/text in a variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can then use the variable and print a message back to them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The output of the above script would be:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First run the script:<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">.\/devdojo.sh<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Then, you would be prompted to enter your name:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>What is your name?\nBobby\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Once you&#8217;ve typed your name, just hit enter, and you will get the following output:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Hi there Bobby\nWelcome to DevDojo!\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To reduce the code, we could change the first <code>echo<\/code> statement with the <code>read -p<\/code>, the <code>read<\/code> command used with <code>-p<\/code> flag will print a message before prompting the user for their input: #!\/bin\/bash read -p &#8220;What is your name? &#8221; name echo &#8220;Hi there $name&#8221; echo &#8220;Welcome to DevDojo!&#8221;Make sure to test this out yourself as well!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash User Input With the previous script, we defined a variable, and we output the value of the variable on the screen with the echo $name. Now let&#8217;s go ahead and ask the user for input instead. To do that again, open the file with your favorite text editor and update the script as follows: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,3],"tags":[],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-bash","category-it-612"],"_links":{"self":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":1,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":39,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/38\/revisions\/39"}],"wp:attachment":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}